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

c++ function template specialization for known size typedefed array

Please consider the following code: #include #include template< typename Type > void func( Type var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; …
j4x
  • 3,595
  • 3
  • 33
  • 64
9
votes
5 answers

What is the best way to create a specialization-only function template?

Is there a better way to do the following? #include template T Bar(); template <> int Bar() { return 3; } // Potentially other specialisations int main() { std::cout << Bar() << std::endl; // This should…
voltrevo
  • 9,870
  • 3
  • 28
  • 33
9
votes
3 answers

c++ function template specialisation

Given this code: class X { public: template< typename T > void func( const T & v ); }; template<> void X::func< int >( const int & v ) { } template<> void X::func< char * >( const char * & v ) // 16 { } When I compile it I get the…
ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43
9
votes
0 answers

Equality of int&'s in template parameters

Suppose we have the following program: template struct Probe { static const int which = 1; }; template struct Probe { static const int which = 2; }; int i = 123; const int myQuestion =…
9
votes
1 answer

Is an implementation required to diagnose ODR-violations of duplicated definitions of the same explicit specialization within the same TU?

Consider a templated entity, say (A) a function template, and (B) a member enum of a class template. // (A) template int f(); // (B) template struct T { enum class E; }; Is an implementation required to diagnose ODR-violations due to…
dfrib
  • 70,367
  • 12
  • 127
  • 192
9
votes
1 answer

clang/gcc inconsistency in class specialization

I came across this issue while trying to specialize tuple_size/tuple_element for a custom class in C++17 for structured binding. Below code compiles in GCC, but not in clang (both trunk versions, see below link). #include…
9
votes
1 answer

Writing a saturate casting operator without listing all possible combinations

I'd like to create templated operations between different types (suppose this is the list: int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t, uint64_t, float, double). Then I'd like to allow for a saturate_cast<>() function to take the…
Costantino Grana
  • 3,132
  • 1
  • 15
  • 35
9
votes
0 answers

Specialization of class template parameter compiles in GCC, Clang and MSVC but GCC selects another specialization

I successfully compile the SSCCE code snippet below in Clang 8.0.0, MSVC v19.20 and GCC 8.3. Both MSVC and Clang will make the program return 0 and GCC causes the program to return 1 (godbolt), so there is a difference and I don't know which…
9
votes
4 answers

Recursive template explanation C++

template int add(ArgTypes... args); template int add(T t, ArgTypes... args) { int sum = 0; return t + add(args...); } template<> int add() { return 0; } How to add more…
9
votes
5 answers

partial specialization of function templates

In the below code snippet, template void func(T1& t) { cout << "all" << endl; } template void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int…
josh
  • 13,793
  • 12
  • 49
  • 58
9
votes
1 answer

Nested class explicit specilization: different compiler behavior

The following code compiles fine with clang++ 6.0.0 and g++ 7.3.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors) but fails to compile with vc++ 19.10.25017 (compilation flag is /Za): template struct A { …
9
votes
4 answers

Template Specialisation with Template Argument

Let's suppose to have a templateclass Foo: template class Foo { void foo(); }; I have another template class Bar (independent from the first one): template class Bar {}; Let's say, I want to specialise the foo() method for…
BiagioF
  • 9,368
  • 2
  • 26
  • 50
9
votes
4 answers

Make C++ fail compilation on specific instantiation of template function

I'm working on a project which has an template function as so: template T foo(T val) { return someFunc(val); } template <> bool foo(bool val) { return otherFunc(val); }; Now, I have a class Bar, which I don't want to accept as…
blackghost
  • 1,730
  • 11
  • 24
9
votes
2 answers

Variadic template specialization with const reference

How to specialize variadic template function that has const reference for an argument? Example: template T foo(Args... args) = delete; template<> int foo(int a, const char* str, const Test& t) { .... } // Fails to…
9
votes
4 answers

Metaprogramming tricks: how to simplify implementation of two metafunctions

I am writing some program to call some APIs automatically through code generation. In some cases I need to convert from a type Source, to a type Target, but these types come decorated with pointers, const, etc. So what I need to do is to remove all…