Last active
December 31, 2015 20:09
-
-
Save Fuyutsubaki/8037801 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<iostream> | |
#include<vector> | |
#include<string> | |
//VSでのみ動作を確認 | |
template<class T> | |
class Piyo | |
{ | |
public: | |
void print() | |
{ | |
for (auto&x : get()) | |
{ | |
std::cout << x << std::endl; | |
} | |
} | |
static std::vector<std::string>&get() | |
{ | |
static std::vector<std::string> p; | |
return p; | |
} | |
template<std::string(*CALL)()> | |
struct Hoge{ | |
Hoge(){x;} | |
struct XXX{ XXX(){ get().push_back(CALL()); } }; | |
static XXX x; | |
}; | |
void func1(){Hoge<_1> c;} | |
void func2(){Hoge<_2> c;} | |
void func3(){Hoge<_3> c;} | |
static std::string _1(){ return"func1"; } | |
static std::string _2(){ return"func2"; } | |
static std::string _3(){ return"func3"; } | |
}; | |
template<class T>template<std::string(*CALL)()> | |
typename Piyo<T>:: template Hoge<CALL>::XXX | |
Piyo<T>::Hoge<CALL>::x; | |
using Foo = Piyo<int>; | |
int main() | |
{ | |
Foo foo; | |
foo.print(); | |
//foo.func1(); | |
foo.func3(); | |
foo.func2(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment