partial-specialization

C++ Partial Specialization ( Function Pointers )

☆樱花仙子☆ 提交于 2019-12-08 04:16:35
问题 Can any one please tell, whether below is legal c++ or not ? template < typename s , s & (*fn) ( s * ) > class c {}; // partial specialization template < typename s , s & (*fn) ( s * ) > class c < s*, s* & (*fn)(s**) {}; g++ ( 4.2.4) error: a function call cannot appear in a constant-expression error: template argument 2 is invalid Although it does work for explicit specialization int & func ( int * ) { return 0; } template <> class c < int , func> class c {}; 回答1: I think you mean template <

C++ specialize template class function without duplicating code

南笙酒味 提交于 2019-12-07 09:23:08
问题 I want to write 5 different classes, each of which has many member functions which are exactly the same, except of one, which is special for each class. Can I write this avoiding code duplication? Regards, Aleksejs Below is a very shortened version of my code, which throws the error: template_test.cpp:15:35: error: invalid use of incomplete type ‘class impl_prototype<cl, 1> #include <iostream> using namespace std; template <int cl, int dim> class impl_prototype { public: impl_prototype() {}

why SFINAE (enable_if) works from inside class definition but not from outside

非 Y 不嫁゛ 提交于 2019-12-07 06:27:09
问题 Very weird problem I've been struggling with for the past few hours (after solving 5-6 other issues with SFINAE as I'm new to it). Basically in the following code I want to have f() working for all possible template instantiations, but have g() available only when N == 2 : #include <type_traits> #include <iostream> template<typename T, int N> class A { public: void f(void); void g(void); }; template<typename T, int N> inline void A<T, N>::f() { std::cout << "f()\n"; } template<typename T, int

Avoiding duplication of function definitions in template specializations

旧时模样 提交于 2019-12-06 20:14:26
问题 The class Widget has some functions that apply for all parameter types (common functions) and other functions that need to be specialized for given types (the uncommon functions). g++ insists that the specialization for Widget should also define common_fn() and not just uncommon_fn(), but that defeats the purpose of using specialization in the first place. How can one avoid repeating common_fn()? #include <cassert> template<typename Type> struct Widget { Widget() {} char common_fn() { return

Static template member function for template class

旧城冷巷雨未停 提交于 2019-12-06 16:15:07
I have a template class and a template member function: template<class T1> struct A{ template<class T2> static int f(){return 0;} }; I want to specialize for a case when T1 and T2 are the same, For example, define the case A<T>::f<T> for any T . but I can't find the combination of keywords to achieve this. How can I partially (?) specialize a combination of template class and a template static function? These are my unsuccessful attempts, and the error messages: 1) Specialize inside the class: fatal error: cannot specialize a function 'f' within class scope ) template<class T1> struct A{

Inner class depending on a template argument

风流意气都作罢 提交于 2019-12-06 13:37:37
问题 Consider next example : #include <iostream> #include <typeinfo> template< int N, typename T > struct B { struct C; }; template< typename T > struct B< 0, T >::C { typedef T type; }; template< int N, typename T > struct B< N, T >::C { typedef T type[N]; }; int main() { std::cout<<"n=0 type = " << typeid( B< 0, float >::C::type ).name() << std::endl; std::cout<<"n=5 type = " << typeid( B< 5, float >::C::type ).name() << std::endl; } When compiled using g++ (version 4.3.0) g++ dfg.cpp -ansi

Partial template specialization for specific type, c++

时光怂恿深爱的人放手 提交于 2019-12-06 09:56:54
问题 Using partial specialization of templates I would like to create a function/method: A) processing only one specific primitive type (int, double, float,...) of the formal parameter and for other types throwing exception template <class T> T min ( Point <T> p ) { /*if (T == int) continue; else throw exception*/ } B) processing more non-primitive types (user defined types) of the formal parameter and for other types throwing exception... Some code examples would be helpful (without c++ boost

Template specialisation with default argument [duplicate]

≯℡__Kan透↙ 提交于 2019-12-06 05:03:01
This question already has answers here : How does `void_t` work (2 answers) Closed last year . I have a program that is as follows. There is a base template struct X and a partial specialisation with SFINAE. template <typename T, typename U = void> struct X{ X() { std::cout << "in 1" << std::endl; }; }; template <typename T> struct X< T, std::enable_if_t<std::is_integral_v<T>> > { X() { std::cout << "in 2" << std::endl; }; }; int main() { X<int> x; } When running the program in 2 is printed. Why is it that the second specialization is chosen over the first since both of them effectively

specializing functions on stl style container types

↘锁芯ラ 提交于 2019-12-06 00:20:20
If i have a type T , what is a useful way to inspect it at compile time to see whether its an STL-style container (for an arbitrary value type) or not? (Assumption: pointers, reference, etc. already stripped) Starting code: template<class T> // (1) void f(T&) {} template<class T> // (2) void f(std::vector<T>&) {} void test() { int a; std::vector<int> b; f(a); f(b); } Now this works fine, but what if i want to generalize the container (i.e. not define (3) , (4) , ... explicitly)? Utilizing SFINAE and typelists would reduce the code somewhat, but is there a better way? Or is there an idiom for

Is it legal to perform partial in-class specialization of a member template class in derived class

大城市里の小女人 提交于 2019-12-05 23:01:20
问题 It is continuation of this question. I am specifically interested if the partial specialization of a member class like this: struct FooParent { template <class> struct Bar{ }; }; struct Foo: FooParent { template <class T> struct Bar<T*> {}; }; I know this can be done inside a namespace scope: template <class T> struct Foo::Bar<T*>{ }; But I'm also specifically interested in in-class partial specialization at the level of derived class. Both clang and gcc complains when encounter a former: