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

Why is the Visual Studio C++ Compiler rejecting an enum as a template parameter?

I'm using the Microsoft Visual Studio 2019 compiler (cl.exe), and it is rejecting some code accepted by both Clang and GCC, related to using enums as template parameters, where the templates are specialized for particular enum values. enum Foo { …
16
votes
1 answer

SFINAE template specialization precedence

#include #include #include template struct trait; template struct trait().begin(), std::declval().end(), void() )> { …
SU3
  • 5,064
  • 3
  • 35
  • 66
16
votes
3 answers

Disambiguate template specialization between map-like and vector-like containers

template struct Printer; // I want this to match std::vector (and similar linear containers) template class T, class TV, class... TS> struct Printer> { ... }; // I want this to match std::map…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
16
votes
1 answer

Define template specialization in cpp?

I can define a specialized function in a cpp like so... // header template void func(T){} template<> void func(int); // cpp template<> void func(int) {} How can I define a method in a specialized class in a cpp? Like so…
David
  • 27,652
  • 18
  • 89
  • 138
15
votes
5 answers

How to enforce use of template specialization?

I'm trying to make my template function produce a compile-time error if the non-specialized base version is instantiated. I tried the usual compile-time assert pattern (negative array size) but the compile is failing even when the template is not…
drwowe
  • 1,975
  • 1
  • 15
  • 17
15
votes
1 answer

Where should I define operator >> for my specialization of std::pair?

Consider the following program: #include #include #include #include using namespace std; //just for convenience, illustration only typedef pair point; //this is my specialization of pair. I call it…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
15
votes
2 answers

Is it ok to write template specializations in a cpp file in this case?

Say I have my code structured this way: header1.h template class C> struct metafunction { using type = typename C::type; }; inline namespace msn { template struct implementation; } // uses the…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
15
votes
3 answers

Why aren't template specializations allowed to be in different namespaces?

Please, see what I am trying to do: #include namespace first { template class myclass { T t; public: void who_are_you() const { std::cout << "first::myclass"; } }; } namespace second { using first::myclass; …
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
15
votes
2 answers

Explicit specialization of function templates causes linker error

Functions.h: #pragma once #include template void TemplatedFunction(T* p) {} template<> void TemplatedFunction(float* p) {} template<> void TemplatedFunction(char* p) {} Functions.cpp: #include "Functions.h" void…
Alex F
  • 42,307
  • 41
  • 144
  • 212
15
votes
1 answer

Definition of a class member in the primary template and an implicit instantiation during specialization

I have the following example that I've decomposed from §14.7.3/6 [temp.expl.spec] that defines a class member enumeration in the primary template and subsequently specializes it. The following doesn't compile in clang: template struct A { …
David G
  • 94,763
  • 41
  • 167
  • 253
15
votes
2 answers

Doxygen for C++ template class member specialization

When I write class templates, and need to fully-specialize members of those classes, Doxygen doesn't recognize the specialization - it documents only the generic definition, or (if there are only specializations) the last definition. Here's a simple…
Ziv
  • 2,369
  • 3
  • 24
  • 40
15
votes
4 answers

Declaration of template class member specialization

When I specialize a (static) member function/constant in a template class, I'm confused as to where the declaration is meant to go. Here's an example of what I what to do - yoinked directly from IBM's reference on template specialization: ===IBM…
Ziv
  • 2,369
  • 3
  • 24
  • 40
15
votes
1 answer

Getting "illegal use of explicit template arguments" when doing a pointer partial specialization for a class method

Hello I'm having problems with partial specialization. What I want to do is have a class that has a template member function that will interpret a given value to one specified by the user. For instance the class name is Value and here is a snippet…
Kunashu
  • 278
  • 1
  • 4
  • 15
14
votes
3 answers

Can I specialize a class template with an alias template?

Here's a simple example: class bar {}; template class foo {}; template <> using foo = bar; Is this allowed?
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
14
votes
5 answers

template specialization for all subclasses

I would like to define a C++ template specialization that applies to all subclasses of a given base class. Is this possible? In particular, I'd like to do this for STL's hash<>. hash<> is defined as an empty parametrized template, and a family of…