template-specialization

C++ Template Specialization with Constant Value

梦想的初衷 提交于 2019-12-17 17:54:33
问题 Is there a straightforward way for defining a partial specialization of a C++ template class given a numerical constant for one of the template parameters? I'm trying to create special constructors for only certain kinds of template combinations: template <typename A, size_t B> class Example { public: Example() { }; A value[B]; }; template <typename A, 2> class Example { public: Example(b1, b2) { value[0] = b1; value[1] = b2; }; }; This example won't compile, returning an error Expected

Understanding (simple?) C++ Partial Template Specialization

℡╲_俬逩灬. 提交于 2019-12-17 17:31:44
问题 Note: this seems to be a repost of a problem: C++ - Overload templated class method with a partial specilization of that method I have boiled down a problem I am having with C++ template specialization down to a simple case. It consists of a simple 2-parameter template class Thing , where I would like to specialize Thing<A,B>::doSomething() for B=int . #include <cstdio> // // A 3-parameter template class. // template <class A, class B> class Thing { public: Thing(A a, B b) : a_(a), b_(b) {} B

Is std::vector<T> a `user-defined type`?

隐身守侯 提交于 2019-12-17 15:39:34
问题 In 17.6.4.2.1/1 and 17.6.4.2.1/2 of the current draft standard restrictions are placed on specializations injected by users into namespace std . The behavior of a C ++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the

Specialized template function with deleted “general” case fails to compile with g++ <=4.8.0 and clang++

无人久伴 提交于 2019-12-17 12:49:51
问题 Compiling a project with an older version of g++ (4.8.0, MinGW) I found that this code fails to compile: template<typename T> void foo() = delete; template<> void foo<int>(){} int main() { foo<int>(); return 0; } It seems that g++ doesn't even try to look for explicit specializations if it sees that the base case is deleted. mitalia@mitalia:~/scratch$ /opt/mingw32-dw2/bin/i686-w64-mingw32-g++ -std=c++11 buggy_deleted_template.cpp buggy_deleted_template.cpp: In function 'int main()': buggy

Why is it not possible to overload class templates?

馋奶兔 提交于 2019-12-17 10:56:37
问题 Reading this question made me wonder: is there a technical reason for disallowing class templates overloads? By overloading, I mean having several templates with the same names, but different parameters, for instance template <typename T> struct Foo {}; template <typename T1, typename T2> struct Foo {}; template <unsigned int N> struct Foo {}; The compiler manages to handle overloaded functions and function templates, wouldn't it be possible to apply the same techniques (e.g. name mangling)

Why function template cannot be partially specialized?

最后都变了- 提交于 2019-12-17 03:03:01
问题 I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

Why function template cannot be partially specialized?

混江龙づ霸主 提交于 2019-12-17 03:02:47
问题 I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template<typename T, typename U> void f() {} //allowed! template<> void f<int, char>() {} //allowed! template<typename T> void f<char, T>() {} //not allowed! template<typename T> void f<T, int>() {} //not allowed! 回答1: AFAIK that's changed in C++0x. I guess it was just an oversight (considering that you can always get the partial

C++ function template partial specialization?

泪湿孤枕 提交于 2019-12-17 02:35:10
问题 I know that the below code is a partial specialization of a class: template <typename T1, typename T2> class MyClass { … }; // partial specialization: both template parameters have same type template <typename T> class MyClass<T,T> { … }; Also I know that C++ does not allow function template partial specialization (only full is allowed). But does my code mean that I have partially specialized my function template for one/same type arguments? Because it works for Microsoft Visual Studio 2010

Template type derivation

雨燕双飞 提交于 2019-12-14 03:08:01
问题 I need to implement a class, say 'MyClass' using templates. template<class T> class MyClass { public: T var1; T1 var2; }; There are two member variables var1 and var2. If the class template argument, 'T', is fundamental type (eg: float, double or long double), the types of both the variables var1 and var2 should be the same as the template argument. That is T1 = T in the above example. But if the template argument is std::complex<T> , I would like to have T var1; std::complex<T> var2; How to

How to specialize a class template for a tuple when variadic template arguments are not supported?

[亡魂溺海] 提交于 2019-12-14 01:28:41
问题 I have a class template template<class T> class A {...}; and I want to specialize it when T is a tuple. I think I can do this template<class Args...> class A<std::tuple<Args...>> {...}; However, my compiler doesn't support variadic template arguments, how to do it? 回答1: You can specialize it for tuples of every different arity: // explicit specialization for 0-element tuples template<> class A<std::tuple<>> {...}; // partial specialization for 1-element tuples template<class Arg> class A<std: