specialization

C++ partial template specialization in combination with std::is_base_of and std::enable_if

試著忘記壹切 提交于 2019-12-03 03:41:38
问题 Let's say I have a two classes: Serializable and Printable . So a simple template function which accepts all derived classes of Printable could look like: template <class T, class B = Printable, class = typename std::enable_if<std::is_base_of<B, T>::value>::type> void print(T value) { cout << value << endl; } However, if I want it to accept also all derived classes of Serializable while I still have control over the function body, this would obviously not work: template <class T, class B =

G++ generates code for unused template specializations?

你离开我真会死。 提交于 2019-12-03 03:25:01
In a bit of serialization code for a project I'm working on I have a type whose size is compiler dependent. In order to deal with this, I decided to use a template specialization, which works great. Everything is resolved at compile time. The code looks a little bit like this (not the real code, just an example): template <int size> void special_function() { std::cout << "Called without specialization: " << size << std::endl; } template <> void special_function<4>() { std::cout << "dword" << std::endl; } template <> void special_function<8>() { std::cout << "qword" << std::endl; } int main() {

Specialize a template with a template

蹲街弑〆低调 提交于 2019-12-02 01:00:16
问题 I have a (free) function template that looks like this template <typename T> T get(); I now want to specialize this function for a class, which itself is a template. But my compiler doesn't want to compile it, and I'm asking now if that is even possible and how I could achieve it. Just for the idea, the code could look as follows: (Doesn't compile) template <> template <typename T> foo_type<T> get<foo_type<T>>() 回答1: What you're doing is called partial specialization of function template. But

Specialize a template with a template

為{幸葍}努か 提交于 2019-12-01 22:03:08
I have a (free) function template that looks like this template <typename T> T get(); I now want to specialize this function for a class, which itself is a template. But my compiler doesn't want to compile it, and I'm asking now if that is even possible and how I could achieve it. Just for the idea, the code could look as follows: (Doesn't compile) template <> template <typename T> foo_type<T> get<foo_type<T>>() What you're doing is called partial specialization of function template. But partial specialization of function template is not allowed. Overloading of function template is allowed,

Specialization of template function after point of use will break the compilation

[亡魂溺海] 提交于 2019-12-01 18:41:46
Consider next example : #include <iostream> template< int a > void foo(); int main(int argn, char* argv[]) { foo<1>(); } template<> void foo<1>() { std::cout<<1<<std::endl; } The compilation fails with next error messages : rg.cpp:12: error: specialization of ‘void foo() [with int a = 1]’ after instantiation What paragraph in the standard explains this error? PS :I know that if I move the function definition in front of main will make the error go away. I think that's undefined behavior according to the standard. There are no restrictions on what a toolchain can do in cases of UB, generating a

Specialization of template function after point of use will break the compilation

时间秒杀一切 提交于 2019-12-01 18:06:04
问题 Consider next example : #include <iostream> template< int a > void foo(); int main(int argn, char* argv[]) { foo<1>(); } template<> void foo<1>() { std::cout<<1<<std::endl; } The compilation fails with next error messages : rg.cpp:12: error: specialization of ‘void foo() [with int a = 1]’ after instantiation What paragraph in the standard explains this error? PS :I know that if I move the function definition in front of main will make the error go away. 回答1: I think that's undefined behavior

Class template specialization in class scope?

◇◆丶佛笑我妖孽 提交于 2019-12-01 15:23:39
Why is the specialization S in A legal and S in B not? ( if B is not commented out ) GCC 4.8.1: error: explicit specialization in non-namespace scope ‘class B’ #include <type_traits> #include <iostream> class Y {}; class X {}; struct A { template<class T, class = void> class S; template<class T> struct S < T, typename std::enable_if< std::is_same< Y, T >::value >::type > { int i = 0; }; template<class T> struct S < T, typename std::enable_if< std::is_same< X, T >::value >::type > { int i = 1; }; }; /* class B { template<class T> class S; template<> class S < Y > {}; template<> class S < X > {}

Class template specialization in class scope?

余生长醉 提交于 2019-12-01 14:17:02
问题 Why is the specialization S in A legal and S in B not? ( if B is not commented out ) GCC 4.8.1: error: explicit specialization in non-namespace scope ‘class B’ #include <type_traits> #include <iostream> class Y {}; class X {}; struct A { template<class T, class = void> class S; template<class T> struct S < T, typename std::enable_if< std::is_same< Y, T >::value >::type > { int i = 0; }; template<class T> struct S < T, typename std::enable_if< std::is_same< X, T >::value >::type > { int i = 1;

template class specialization with template class parameter

≯℡__Kan透↙ 提交于 2019-12-01 09:38:14
Say I have : template < typename T > class ClassA { void doTheStuff (T const * t); }; template < typename T > class ClassB { // Some stuff... }; I'd like to specialize the doTheStuff method for all instances of the ClassB template like this: template <typename T> void ClassA< ClassB< T > >::doTheStuff (ClassB< T > const * t) { // Stuff done in the same way for all ClassB< T > classes } But of course, this doesn't work. Shame is I don't know how I could do that. With visual studio's compiler I get: error C2244: 'ClassA<T>::doTheStuff' : unable to match function definition to an existing

template specialization of template class

扶醉桌前 提交于 2019-12-01 04:08:37
I want to specialize following member function: class foo { template<typename T> T get() const; }; To other class bar that depends on templates as well. For example, I would like bar to be std::pair with some template parameters, something like that: template<> std::pair<T1,T2> foo::get() const { T1 x=...; T2 y=...; return std::pair<T1,T2>(x,y); } Where T1 and T2 are templates as well. How can this be done? As far as I know it should be possible. So now I can call: some_foo.get<std::pair<int,double> >(); The full/final answer: template<typename T> struct traits; class foo { template<typename T