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
33
votes
4 answers

How to know if a type is a specialization of std::vector?

I've been on this problem all morning with no result whatsoever. Basically, I need a simple metaprogramming thing that allows me to branch to different specializations if the parameter passed is a kind of std::vector or not. Some kind of is_base_of…
Michael
  • 1,357
  • 3
  • 15
  • 24
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
  • 77,323
  • 27
  • 116
  • 141
29
votes
2 answers

Specialization of member function template after instantiation error, and order of member functions

The following bit of code fails to compile on gcc 4.5.3 struct Frobnigator { template void foo(); template void bar(); }; template void Frobnigator::bar() { } template void…
Olumide
  • 5,397
  • 10
  • 55
  • 104
28
votes
2 answers

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

Quote from cppreference.com: Adding template specializations It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
28
votes
1 answer

C++ inconsistency between gcc and clang

I came across a C++ inconsistency between gcc (versions 4.8.1, 4.8.2) and clang (versions 3.3, 3.4). I wonder which one is correct. Here's the program: template < typename T > struct Result {}; template < typename T > struct Empty {}; template <…
Matei David
  • 2,322
  • 3
  • 23
  • 36
27
votes
4 answers

No class template specialization for array of bool?

According to https://en.cppreference.com/, std::vector has a class template specialization, while std::array does not. Which are the reasons why it is not provided?
26
votes
2 answers

Specialization of templated member function in templated class

I have a templated class with an templated member function template class A { public: template CT function(); }; Now I want to specialize the templated member function in 2 ways. First for having the same type as the…
Nathan
  • 7,099
  • 14
  • 61
  • 125
26
votes
2 answers

Nested class template specialization

A class: template class A { template class Nested{}; Nested n; }; And I want to specialize Nested. Here what I tried: template class A { template
nikitablack
  • 4,359
  • 2
  • 35
  • 68
25
votes
1 answer

C++ template specialization of function: "illegal use of explicit template arguments"

The following template specialization code: template void spec1() { } Test case 1: template< typename T1> //compile error void spec1() { } Test case 2: template< typename T2> //compile error void…
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
25
votes
3 answers

Partial specialization of a method in a templated class

Given: struct A { virtual bool what() = 0; }; template struct B : public A { virtual bool what(); }; I want to partially specialize what like: template bool B::what() { return…
David
  • 27,652
  • 18
  • 89
  • 138
24
votes
1 answer

Good practices regarding template specialization and inheritance

Template specialization does not take into account inheritance hierarchy. For example, if I specialize a template for Base and instantiate it with Derived, the specialization will not be chosen (see code (1) below). This can be a major hindrance,…
Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
24
votes
3 answers

Why can't we specialize concepts?

The syntax that works for classes does not work for concepts: template concept C = requires(Type t) { // ... }; template concept C = requires(Type t) { // ... }; MSVC says for the line of the…
Dr. Gut
  • 2,053
  • 7
  • 26
24
votes
4 answers

Multiple typename arguments in c++ template? (variadic templates)

How can I have multiple typename arguments in a c++ template? #ifndef _CALL_TEMP_H #define _CALL_TEMP_H #include #include template class Foo; template class Foo { public: …
Jichao
  • 40,341
  • 47
  • 125
  • 198
23
votes
4 answers

Is it possible to implement always_false in the C++ standard library?

There are cases where one uses an always_false helper to e.g. cause unconditional static_assert failure if instantiation of some template is attempted: template struct always_false : std::false_type {}; template struct…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72
23
votes
2 answers

function template specialization compile error

##A.hh template void func(T t) {} template<> void func(int t) {} void func2(); ##A.cpp void func2() {} ##main.cpp func("hello"); func(int()); The error I get is: error LNK2005: "void __cdecl func(int)" (??$func@H@@YAXH@Z)…
hidayat
  • 9,493
  • 13
  • 51
  • 66