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).

1435 questions
94
votes
6 answers

C++ function template partial specialization?

I know that the below code is a partial specialization of a class: template class MyClass { … }; // partial specialization: both template parameters have same type template class MyClass { ……
Narek
  • 35,407
  • 69
  • 202
  • 359
90
votes
4 answers

Why function template cannot be partially specialized?

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 void f() {} //allowed! template<> void f
77
votes
2 answers

"template<>" vs "template" without brackets - what's the difference?

Suppose I've declared: template void foo(T& t); Now, what is the difference between template <> void foo(int& t); and template void foo(int& t); semantically? And do template-with-no-brackets and…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
62
votes
3 answers

Template Specialization VS Function Overloading

A textbook I have notes that you can provide your own implementation for standard library functions like swap(x,y) via template specialization or function overloading. This would be useful for any types which can benefit from something other than…
54
votes
2 answers

C++ templates specialization syntax

In C++ Primer Plus (2001, Czech Translation) I have found these different template specialization syntax: function template template void foo(T); specialization syntax void foo(int param); // 1 void foo(int param); // 2 template…
Jan Turoň
  • 26,696
  • 21
  • 102
  • 153
48
votes
3 answers

Why is it not possible to overload class templates?

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 struct Foo…
44
votes
1 answer

Partial specialization of variadic templates

Consider the following class template 'X' and its partial specializations. template struct X {}; // #1 template struct X {}; // #2 template struct X
43
votes
6 answers

Why are there so many specializations of std::swap?

While looking at the documentation for std::swap, I see a lot of specializations. It looks like every STL container, as well as many other std facilities have a specialized swap. I thought with the aid of templates, we wouldn't need all of these…
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
43
votes
2 answers

std::remove_reference explained?

I saw possible implementations for std::remove_reference as below template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference {typedef T type;}; template< class T > struct…
Koushik Shetty
  • 1,986
  • 4
  • 17
  • 30
40
votes
4 answers

Is std::vector a `user-defined type`?

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 …
Yakk - Adam Nevraumont
  • 235,777
  • 25
  • 285
  • 465
38
votes
5 answers

Circumventing template specialization

Suppose I am a user of a Certain Template Library (CTL) which defines a template, named, say, Hector template class Hector {...}; And in its documentation it gives many guarantees about Hector template behavior. But then it also defines…
Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
38
votes
6 answers

C++ Template Specialization with Constant Value

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…
tadman
  • 194,930
  • 21
  • 217
  • 240
34
votes
6 answers

C++ specialization of template function inside template class

What is the C++ syntax for specializing a template function that's inside a template class? For example, consider that I have the following two classes and their usage. I would like to be able to provide specialized implementations of the method…
Ogre Psalm33
  • 19,630
  • 16
  • 73
  • 90
32
votes
1 answer

C++ template specialization, calling methods on types that could be pointers or references unambiguously

Summary Is there a way to call a class method on a templated type that could be a pointer or a reference without knowing which and not get compiler/linker errors? Details I have a templated QuadTree implementation that can take any of the following…
Casey
  • 8,686
  • 9
  • 52
  • 79
30
votes
1 answer

Function template specialization format

What is the reason for the second brackets <> in the following function template: template<> void doh::operator()<>(int i) This came up in SO question where it was suggested that there are brackets missing after operator(), however I could not find…
stefanB
  • 69,149
  • 26
  • 113
  • 140
1
2 3
95 96