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

Syntax for specialization of nested template class

I'm trying to figure out the correct syntax for explicit specialization of a nested template class. The following code will better illustrate: struct Column_Major; struct Row_Major; template
13
votes
2 answers

Template specialization with type conversion

I have found this piece of bogus code (contrived example below): template struct foo { static int bar() { return 1; } }; template struct foo { static int bar() …
magor
  • 359
  • 1
  • 11
13
votes
1 answer

Template class member specialization without declaration in header

I have a template class that I declare in a header with one method and no definition of that method in the header. In a .cc file, I define specializations of that method without ever declaring them in the header. In a different .cc file, I call the…
12
votes
1 answer

Compile-time elimination of if/else branch in C++

In the following code sample, the if statement depends on bool template parameter, which is a compile-time constant. Compilers handle this code differently: MSVC fails with link error (which is what I expected), because the template function in…
12
votes
1 answer

c++ template specialization - linker error multiple definitions

My third question here today ;-), but I am really new to c++ template programming and operator overloading. I am trying the following: terminallog.hh //snipped code class Terminallog { public: Terminallog(); Terminallog(int); virtual…
ftiaronsem
  • 1,524
  • 4
  • 19
  • 32
12
votes
1 answer

GCC/CLang disagree on partial specialization of template template parameter

GCC and clang disagree about this code. #include template typename Tpl> struct storage { using type_t = T; template using storage_tpl = Tpl; }; template…
dvd
  • 1,014
  • 6
  • 12
12
votes
1 answer

Ambiguous partial specializations with Clang in C++17

template struct TSelect {}; enum What { The }; template struct AnotherOneSelector { static constexpr Foo Id = Foo::The; }; template struct THelper; template…
D. Kutenin
  • 123
  • 7
12
votes
2 answers

How to specialize classes for all reference types C++03

Please notice C++03 is what I really need, but for knowledge sake, I would like to see some more pretty implementations in C++11 as well. I need a template class template class A { private: T m_member; public: A(T _member); …
Or B
  • 1,675
  • 5
  • 20
  • 41
12
votes
3 answers

How to specialize std::begin?

I'm trying to specialize std::begin for a custom container. I'm doing this because I want to use range-based for with the container. This is what I have: class stackiterator { … }; class stack { … }; #include template <> stackiterator…
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
12
votes
3 answers

How to initialize a static std::unordered_map of a type trait?

Given the following type trait, how can I initialize Fields with some std::pairs? template <> struct ManagerDataTrait { static const std::unordered_map Fields; // ... }; I tried using a lambda but Visual…
danijar
  • 32,406
  • 45
  • 166
  • 297
12
votes
5 answers

Specializing std::optional

Will it be possible to specialize std::optional for user-defined types? If not, is it too late to propose this to the standard? My use case for this is an integer-like class that represents a value within a range. For instance, you could have an…
David Stone
  • 26,872
  • 14
  • 68
  • 84
12
votes
3 answers

Why does this code give the error, "template specialization requires 'template<>'"?

When I try to compile this with Clang template struct Field { char const *name; Field(char const *name) : name(name) { } }; template class CRTP { static Field const _field; }; class Class : public…
user541686
  • 205,094
  • 128
  • 528
  • 886
12
votes
1 answer

class template state data member, not an entity that can be explicitly specialized

I got an error in the code below: template::value> class class_name; template<> class class_name{ public: static string const value; }; template<> string const class_name
11
votes
6 answers

c++ template specialization for all subclasses

I need to create a template function like this: template void foo(T a) { if (T is a subclass of class Bar) do this else do something else } I can also imagine doing it using template specialization ... but I have never…
Abhishek Anand
  • 3,789
  • 2
  • 21
  • 29
11
votes
4 answers

specializing iterator_traits

I'd like to specialize std::iterator_traits<> for iterators of a container class template that does not have the usual nested typedefs (like value_type, difference_type, etc.) and whose source I shouldn't modify. Basically I'd like to do something…
imre
  • 1,667
  • 1
  • 14
  • 28