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
17
votes
2 answers

Specializing a template class as a struct

I was just over specializing std::hash for a user-defined type using: template<> struct hash<...> {...}; When VC10 greeted me with the warning: warning C4099: 'std::hash<_Kty>': type name first seen using 'class' now seen using 'struct' and I…
Christian Rau
  • 45,360
  • 10
  • 108
  • 185
17
votes
2 answers

Template specializations in friend declarations post C++20

Context: To our surprise, MSVC with C++20 mode and two-phase compliance enabled accepts the following code: template class X { friend int foo(X x); int a = 10; }; template int foo(T t) { return t.a; } int…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
17
votes
3 answers

Template specialization for an empty parameter pack

I have a variadic template function which calls itself to determine the largest number in a list (constituted by the templatized arguments). I am trying to make a specialization for when the parameter pack is empty so I can just return the number at…
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
17
votes
4 answers

Template member function specialization in a template class

I have a template class and a member function print() to print the data. template class A { public: T data; void print(void) { std::cout << data << std::endl; } // other functions ... }; Then, I want to either…
17
votes
1 answer

Is Clang correct to reject code in which the nested class of a class template is defined only via specializations?

Given the following class template: template struct Outer { struct Inner; auto f(Inner) -> void; }; we define Inner separately for each specialization of Outer: template<> struct Outer::Inner {}; template<> struct…
17
votes
2 answers

Template factorial function without template specialization

I don't understand the following behavior. The following code, aimed at computing the factorial at compile time, doesn't even compile: #include using namespace std; template int f() { if (N == 1) return 1; // we exit the…
Enlico
  • 23,259
  • 6
  • 48
  • 102
17
votes
5 answers

How to conditionally add a function to a class template?

I have a Matrix class template as follows: template class Matrix { T data[nrows][ncols]; public: T& operator ()(std::size_t i, std::size_t j) { return data[i][j]; } }; What I…
user4085386
17
votes
4 answers

Class template specializations with shared functionality

I'm writing a simple maths library with a template vector type: template class Vector { public: Vector &operator+=(Vector const &other); // ... more operators, functions ... }; Now I want some…
Thomas
  • 174,939
  • 50
  • 355
  • 478
17
votes
3 answers

C++ different template methods called on same variable

Could someone explain why once is used method c(T*) and next time d<>(int*) ? methods c and d seems identical to me and I cannot figure out why is not the same type of method called. #include using namespace std; template
zlenyk
  • 922
  • 2
  • 7
  • 22
17
votes
2 answers

Why does the Standard prohibit friend declarations of partial specializations?

The C++ standard prohibits friend declarations of partial specializations. (§14.5.3/8): Friend declarations shall not declare partial specializations. [Example: template class A { }; class X { template friend class A; …
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
16
votes
2 answers

Specializing a variadic template template parameter on the minimum number of arguments: legal or not?

I have code: #include template class> struct Foo { enum { n = 77 }; }; template class C> struct Foo { enum { n = 99 }; }; template struct A {…
glaebhoerl
  • 7,695
  • 3
  • 30
  • 41
16
votes
4 answers

How can I use template specialization in c++ classes, and why this doesn't compile?

I am working on a XmlWriter class, and I wanted to be able to output attributes or text in most standard data formats (strings, integers, floating point numbers etc). To achieve this, I am using a file stream. For the bool data type, I wanted to…
Tibi
  • 4,015
  • 8
  • 40
  • 64
16
votes
2 answers

Why the linker complains about multiple definitions in this template?

This little piece of code triggers the linker's anger when included on at least two translation units (cpp files) : # ifndef MAXIMUM_HPP # define MAXIMUM_HPP template T maximum(const T & a, const T & b) { return a > b ? a : b…
overcoder
  • 1,523
  • 14
  • 24
16
votes
2 answers

Is it OK to inject a specialization into the std namespace?

In this article on defining your own extensions to ::std::error_code the author recommends this code: namespace std { template <> struct is_error_code_enum : public true_type {}; } in order to enable conversions from your own…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
16
votes
5 answers

How to specialize Iterator by its value type, in C++?

Is it possible to specialize an Iterator template parameter by its value_type? I have a function with the following prototype. template void f(InputIterator first, InputIterator last); And I want to handle specially if…
niboshi
  • 1,448
  • 3
  • 12
  • 20