specialization

Partial specialization with type nested in a templated class

巧了我就是萌 提交于 2019-12-04 06:07:39
I'm playing with templates and partial specialization, but there is one specialization I don't know how to write... I'll simplify code to make it easier to read. Let's condiser template <typename T> class x { ... }; Usually, I can specialize like this : class x<a_type> { ... }; Also works with templates types : template <typename T> class x<std::vector<T>> { ... } Now I would like to make the specialization for a type nested in a templated class: template <typename T> class y { struct nested_type { y a_member; }; ... }; // Here comes the specialization template <typename T> class x<y<T>:

Template specialization with a templatized type

a 夏天 提交于 2019-12-04 03:08:42
I want to specialize a class template with the following function: template <typename T> class Foo { public: static int bar(); }; The function has no arguments and shall return a result based on the type of Foo. (In this toy example, we return the number of bytes of the type, but in the actual application we want to return some meta-data object.) The specialization works for fully specified types: // specialization 1: works template <> int Foo<int>::bar() { return 4; } // specialization 2: works template <> int Foo<double>::bar() { return 8; } // specialization 3: works typedef pair<int, int>

C++ linking and template specializations

 ̄綄美尐妖づ 提交于 2019-12-04 02:12:57
I'm studying the behavior of the C++ linker with respect to template specializations. I'm using Microsoft Visual C++ 2010 for these experiments. I don't know if the behavior is the same with other toolchains (e.g. gcc). Here's a first code snippet: // bar.cpp template <typename T> int foo() { return 1; } int bar() { return foo<double>(); } // main.cpp template <typename T> int foo() { return 1; } template <> int foo<double>() { return 2; } int bar(); int main() { const int x = bar(); const int y = foo<double>(); // doesn't link } Expectedly, this code doesn't link because foo<double>() has

C++ template class specialization: why do common methods need to be re-implemented

只愿长相守 提交于 2019-12-04 01:34:19
In the sample: #include <iostream> using namespace std; class B { public: virtual void pvf() = 0; }; template <class T> class D : public B { public: D(){} virtual void pvf() {} private: string data; }; template <> class D<bool> : public B { public: D(); virtual void pvf(){ cout << "bool type" << endl; } }; int main() { D<int> d1; D<bool> d2; } I get the following error: test.cpp:(.text+0x1c): undefined reference to `D<bool>::D()' Note that the reason I don't just specialize the D() by itself is I want to eliminate the need for string D<T>::data in the D<bool> case. Why do I need to re

Partial Specialization of Operator()

妖精的绣舞 提交于 2019-12-03 16:49:47
One of my classes declares a templated function: template<class A, class B> A do_something(const std::vector<B> &data) which I'd like to partially specialize on typename A . B is a family of types that implement a pretty minimal interface, and we use a lot of them, so I'd like my specialization to be generic on B . I suspect this is doubly vexing as typename A is used only as the return type. From the internet, I've gleaned that I can't partially specialize a function, so I've created a class as follows: template<class A, class B> class do_something_implementation { public: do_something

G++ generates code for unused template specializations?

浪尽此生 提交于 2019-12-03 13:08:21
问题 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" <<

Template Specialization of Function inside of a Templated Class

故事扮演 提交于 2019-12-03 08:35:08
I have a templated class and inside I have a templated function( different template parameters ) and I having issues getting the compiler to call the correct one. Example: template< class Parm1, class Parm2, class Parm3 > class Class { public: void Func( Parm1 arg1, Parm2 arg2 ) { Call<Parm3>( arg1, arg2 ); } protected: template< class Type > void Call( Parm1 arg1, Parm2 arg2 ) { } template<> void Call<void>( Parm1 arg1, Parm2 arg2 ) { } }; So if the type of Parm3 is 'void' I want the second Call to be called. Otherwise the first. VS it works fine but GCC pukes all over it. It always calls the

Why are function template specializations not allowed inside a class?

☆樱花仙子☆ 提交于 2019-12-03 06:58:52
After having found answers to many of my questions on stackoverflow, I have now come up against a question of which I can't find the answer and I hope that someone is willing to help me! My problem is that I want to do an explicit templatization of a function inside a class in C++. My compiler (g++) and a look in the C++ standard (§14.7.3) tells me that this specialization has to be done in the namespace in which the class is declared. I understand that this implies that I cannot put the specialization inside the class, but I don't see the point of this restriction! Does anyone know if there

C++ template specialization of constructor

浪尽此生 提交于 2019-12-03 06:48:08
I have a templated class A<T, int> and two typedefs A<string, 20> and A<string, 30>. How do I override the constructor for A<string, 20> ? The following does not work: template <typename T, int M> class A; typedef A<std::string, 20> one_type; typedef A<std::string, 30> second_type; template <typename T, int M> class A { public: A(int m) {test= (m>M);} bool test; }; template<> one_type::one_type() { cerr << "One type" << endl;} I would like the class A<std::string,20> to do something that the other class doesn't. How can I do this without changing the constructor A:A(int) ? The only thing you

C++ template specialization without default function

跟風遠走 提交于 2019-12-03 05:38:21
问题 I have the following code that compiles and works well: template<typename T> T GetGlobal(const char *name); template<> int GetGlobal<int>(const char *name); template<> double GetGlobal<double>(const char *name); However I want to remove the "default" function. That is, I want to make all calls to GetGlobal<t> where 't' is not an int or a double an error. For example, GetGlobal<char>() should be a compile time error. I tried to just delete the default function, but, as I imagined, I received a