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
14
votes
3 answers

enable_if method specialization

template struct A { A operator%( const T& x); }; template A A::operator%( const T& x ) { ... } How can I use enable_if to make the following specialization happen for any floating point type…
David
  • 27,652
  • 18
  • 89
  • 138
14
votes
3 answers

Why do templates specialisations need to be inlined?

I am referring to this answer: https://stackoverflow.com/a/4447057/930315 I ran into a similar issue as the OP of the cited question, having a function template void func(T& val); and its specialization template<> void…
14
votes
1 answer

Default argument for partial specialization [Clang yes, GCC no]

Why does the following compile with clang but not with g++ 4.9 #include template< typename T1, typename T2 , typename T3 = int> struct A; template struct A< std::array, T2, T3 >…
Gabriel
  • 8,990
  • 6
  • 57
  • 101
14
votes
3 answers

g++ Bug with Partial Template Specialization

I am writing some TMP-heavy code for g++ (version 4.8.1_1, Macports) and clang++ (version 3.3, Macports). While g++ rejects the following code listing with UNBRIDLED FURY, clang++ compiles it with grace and splendor. Which complier is in the right?…
void-pointer
  • 14,247
  • 11
  • 43
  • 61
14
votes
1 answer

Partial specialization of templates with integer parameters

I'm trying to do some partial specialization stuff. I have a tuple, and I want to iterate from a certain element index to the first tuple index, accumulating a value from each type in the tuple. This would seem to be a simple matter of using a…
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
13
votes
1 answer

Partial template specialization ambiguity

I cant see why the statement in main is ambiguous. template struct X { void f() { cout << "Primary template" << endl; } }; template struct X {void f() { cout << "Partial specialization 1" <<…
13
votes
1 answer

Is it guaranteed that a specialization of std::numeric_limits for user-defined numeric type S works for cv-qualified S out of the box?

I have a user-defined numeric type S for which I specialized std::numeric_limits. Although I specialized for S only, my custom max() is also used for cv-qualified S, at least with recent versions of gcc and MSVC. Is this guaranteed to work, or am…
Tom
  • 185
  • 2
  • 2
  • 12
13
votes
2 answers

Partial specialisation of member function with non-type parameter

I have a template class with both a type and a non-type template parameter. I want to specialize a member function, what I finding is, as in the example below, I can do a full specialization fine. template struct foo { …
Eoin
  • 1,709
  • 11
  • 22
13
votes
1 answer

Can I define templates for different sets of types?

I need to write a templated function, that behaves differently depending on the class of its parameter: template bool myFunc(ContainerType in){ //do some stuff } template bool myFunc(NotAContainerType…
Jonn Dove
  • 477
  • 2
  • 11
13
votes
3 answers

How to specialize only some members of a template class?

Code: template struct A { void f1() {}; void f2() {}; }; template<> struct A { void f2() {}; }; int main() { A data; data.f1(); data.f2(); }; ERROR: test.cpp: In function 'int main()': test.cpp:16: error: 'struct…
13
votes
1 answer

How to implement std::distance() for custom templated iterator?

I have a templated bidirectional iterator. I do not want to make it random access because the it += n operation would not be constant time. However, the it2 - it1 operation is constant time. I wanted to specialize std::distance() for this…
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
13
votes
3 answers

Specializing function template for reference types

Why is the output of this code : #include template void f(T param) { std::cout << "General" << std::endl ; } template<> void f(int& param) { std::cout << "int&" << std::endl ; } int main() { float x ; …
13
votes
3 answers

Is it legal to partially specialise variadic template inner class with args from variadic template of an outer class

Consider the code: #include template struct outer { template struct inner { static constexpr bool value = false; }; template struct inner { static…
W.F.
  • 13,888
  • 2
  • 34
  • 81
13
votes
3 answers

using template specialization

Usual template structs can be specialized, e.g., template struct X{}; template<> struct X{}; C++11 gave us the new cool using syntax for expressing template typedefs: template using YetAnotherVector =…
gexicide
  • 38,535
  • 21
  • 92
  • 152
13
votes
4 answers

Is size_t guaranteed to be an alias type to one of integer types?

Or can it be a separate unsigned integer type? I have different specializations of a template function for different (unsigned) integer types. Do I need to provide a separate specialization for size_t?
user2052436
  • 4,321
  • 1
  • 25
  • 46