Last active
December 11, 2015 03:19
-
-
Save Twinklebear/4536729 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <string> | |
#include <iostream> | |
template<class T> | |
class SillyTest { | |
public: | |
//Give the object something to say | |
SillyTest(T obj) : mObj(obj){} | |
//Make use of the stored function | |
void Speak(){ | |
mFunc(mObj); | |
} | |
private: | |
T mObj; | |
const static std::function<void(T)> mFunc; | |
}; | |
typedef SillyTest<bool> SillyBool; | |
//Having this commented out lets msvc compile it, | |
//g++ still refuses and i only have clang 3.0 (no lambda support) | |
//template<> | |
const std::function<void(bool)> | |
SillyBool::mFunc = [](bool b){ | |
std::cout << "SillyBool is: " | |
<< (b ? "true" : "false") << std::endl; | |
}; | |
typedef SillyTest<std::string> SillyString; | |
//Having this commented out lets msvc compile it, | |
//g++ still refuses and i only have clang 3.0 (no lambda support) | |
//template<> | |
const std::function<void(std::string)> | |
SillyString::mFunc = [](std::string s){ | |
std::cout << "SillyString is: " << s << std::endl; | |
}; | |
int main(int argc, char const *argv[]){ | |
SillyBool sBool(false); | |
SillyString sString("Howdy doo"); | |
sBool.Speak(); | |
sString.Speak(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment