c++ - Class template as parameter in a template specializationa -
c++ - Class template as parameter in a template specializationa -
is possible utilize class template parameter in template specialization?
i able utilize foo<foo<int>>()
(see #3
in source code) , have unique code template instance run. @ moment ordinary specialization works (see #2
).
a previous similar question have me believe approach #3
work, code doesn't work under msvc2012 @ least.
is i'm trying possible? if so, how?
source
// test struct. template<class t> struct foo { t foo; }; // #1 ordinary template template<class t> t foo() { homecoming t(); } // #2 template specialization template<> int foo<int>() { homecoming 42; } // #3 template specialization template parameter? not working. template<> template<typename t> foo<t> foo<foo<t>>() { homecoming foo<t>(); }
functions can't partially specialized, need wrap class or struct
#include <iostream> using namespace std; // test struct. template<class t> struct foo { t foo; }; // struct specialization template<> struct foo<bool> { static const int val = 46; }; // #1 ordinary template template<class t> struct functionwrapper { static t foo() { homecoming t(); } }; // #2 template specialization template<> struct functionwrapper<int> { static int foo() { homecoming 42; } }; // #3 template specialization template parameter template<class t> struct functionwrapper<struct foo<t>> { static foo<t>* foo() { homecoming new foo<t>(); } }; int main() { cout << functionwrapper<bool>::foo() << endl; cout << functionwrapper<int>::foo() << endl; foo<bool> *obj = functionwrapper<foo<bool>>::foo(); cout << obj->val; delete obj; // citizen homecoming 0; }
http://ideone.com/8txjh4
c++ templates template-specialization
Comments
Post a Comment