Questions tagged [template-specialization]

Template specialization refers to programmer-generated explicit specialization of templates for specific types.

Templates refer to the ability to write code that works for any type satisfying the operations used within the class and/or function on that type. Template specialization refers to the ability for the programmer to explicitly change the code for a given type, either by a partial specializion of the template (in which a subset of parameters are specialized over), or a full specialization of the template (where all parameters are specialized).

1616 questions
23
votes
2 answers

Multiple SFINAE class template specialisations using void_t

Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member…
22
votes
2 answers

Understanding (simple?) C++ Partial Template Specialization

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…
Dan
  • 1,258
  • 1
  • 10
  • 22
22
votes
3 answers

Enforce template type through static_assert

I'm trying to understand the usefulness of static_assert, and I want to know if it can help me in enforcing a design, and if so, how. I have a general template class that hides its own implementation inside another template class which is partially…
Zeenobit
  • 4,954
  • 3
  • 34
  • 46
20
votes
1 answer

Template specialization with variadic templates

template void doStuff(Params...) { } template <> void doStuff(int, bool) { } int main(int, char**) { doStuff<1,int,bool>(1, false); return 0; } This doesn't compile, the second…
coyotte508
  • 9,175
  • 6
  • 44
  • 63
20
votes
2 answers

Conversion operator template specialization

Here's a largely academic exercise in understanding conversion operators, templates and template specializations. The conversion operator template in the following code works for int, float, and double, but fails when used with std::string... sort…
Nathan
  • 4,777
  • 1
  • 28
  • 35
20
votes
4 answers

C++ template specialization on functions

I'm playing around with template specialization, and I've found an issue I can't seem to solve; this is my code: template void test(T* array) { ... test(array); } template void test<0>(T*…
Skeen
  • 4,614
  • 5
  • 41
  • 67
20
votes
2 answers

How to decide if a template specialization exist

I would like to check if a certain template specialization exist or not, where the general case is not defined. Given: template struct A; // general definition not defined template <> struct A {}; // specialization defined for…
Fabio
  • 2,105
  • 16
  • 26
19
votes
2 answers

Explicit specialization after instantiation

I have the following code: typedef vector Vec; typedef vector VecOfVec; template Vec DoSomething(const Vec &v); template<> VecOfVec DoSomething(const VecOfVec &v) { VecOfVec r; for(auto i = v.begin(); i !=…
Daniel
  • 30,896
  • 18
  • 85
  • 139
19
votes
1 answer

Call non-specialised template class function from specialized template class function

Is it possible to call a function defined in a non-specialised template class from a specialised template class? Here is an example of what i am attempting: template struct Convert { static inline void toString(unsigned num, unsigned…
Graeme
  • 4,514
  • 5
  • 43
  • 71
19
votes
2 answers

Is it possible to specialize a template using a member enum?

struct Bar { enum { Special = 4 }; }; template struct Foo {}; template struct Foo {}; Usage: Foo aa; fails to compile using gcc 4.1.2 It complains about the usage of T::Special for partial…
Altan
  • 221
  • 3
  • 5
19
votes
1 answer

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

Compiling a project with an older version of g++ (4.8.0, MinGW) I found that this code fails to compile: template void foo() = delete; template<> void foo(){} int main() { foo(); return 0; } It seems that g++ doesn't…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
19
votes
1 answer

C++14 warning: too many template headers for variable (should be 0)

While experimenting with the recent g++-5 compiler, I wrote below statement in a file: template T a; template<> int a = 1; Which results in: warning: too many template headers for a (should be 0) Also effectively, it doesn't really specialize…
iammilind
  • 68,093
  • 33
  • 169
  • 336
18
votes
3 answers

Can variadic template template parameter be partial-specialized?

Consider the following program: template class> struct foo {}; template class C> struct foo {}; int main() {} Clang rejects it with error: class template partial specialization does not specialize any…
llllllllll
  • 16,169
  • 4
  • 31
  • 54
18
votes
3 answers

How std::conditional works

We have this little metaprogramming marvel called std::conditional described here. In the same reference it says that a possible implementation is template struct conditional { typedef T type; }; template
18
votes
1 answer

Class template specialization priority/ambiguity

While trying to implement a few things relying on variadic templates, I stumbled accross something I cannot explain. I boiled down the problem to the following code snippet: template struct A {}; template