Skip to content

Instantly share code, notes, and snippets.

@Twinklebear
Last active December 11, 2015 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Twinklebear/4536729 to your computer and use it in GitHub Desktop.
Save Twinklebear/4536729 to your computer and use it in GitHub Desktop.
#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