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
11
votes
1 answer

Why does Clang prefer the primary template over the specialization from C++17?

The following program is reduced from the code in this question: template struct S; template struct S {}; S s; In all versions of GCC, in all…
cigien
  • 57,834
  • 11
  • 73
  • 112
11
votes
4 answers

Is an entirely new class compiled for each differently sized std::array?

As far as I understand, c++ templating works by compiling a separate class or function for every type needed. This seems logical for classes or functions that will only be called for a handful of different types / parameters, but for std::array it…
JShorthouse
  • 1,450
  • 13
  • 28
11
votes
1 answer

g++ and clang++ different behaviour with template specialization for auto argument

Playing with C++17 auto template arguments I've encountered another g++/clang++ disagreement. Given the following simple code template struct foo; template struct foo { }; int main () { foo<42l> f42; // <--- long constant,…
max66
  • 65,235
  • 10
  • 71
  • 111
11
votes
3 answers

Java Generics, support "Specialization"? Conceptual similarities to C++ Templates?

I know quite a bit how to use C++-Templates -- not an expert, mind you. With Java Generics (and Scala, for that matter), I have my diffuculties. Maybe, because I try to translate my C++ knowledge to the Java world. I read elsewhere, "they are…
towi
  • 21,587
  • 28
  • 106
  • 187
11
votes
3 answers

Understanding the declaration, definition and specialization of templates

I'm trying to understand the below example, but I'm a bit confused from the three different template and struct declarations. Could you please describe what will happen for the below call? which of the templates will be used and when? Also why does…
11
votes
1 answer

Clang won't compile a template specialization that gcc will

Gcc compiles this fine, but Clang (trunk) refuses with the message: :7:8: error: class template partial specialization is not more specialized than the primary template…
xaxxon
  • 19,189
  • 5
  • 50
  • 80
11
votes
3 answers

Call the unspecialized version of a function when specializing it in C++?

Say I have a templated class: template class foo { void do_someting(T obj) { // do something generic... } }; and I want to specialize do_something, but within it I want to call the "normal" do_something…
Jacob B
  • 2,005
  • 13
  • 12
11
votes
2 answers

How to determine the primary template of a function specialization?

It is usually quite intuitive what the primary template of a function template specialization is, however, I am looking for the formal rules to understand the more surprising situations. For example: template void f(T, U) {}…
user1494080
  • 2,064
  • 2
  • 17
  • 36
11
votes
1 answer

Specialize static constexpr data member

I have a class to describe some traits of a type. template struct my_traits { static constexpr int some_trait = 0; static constexpr T min() { return std::numeric_limtis::min(); } static constexpr T max() { return…
maddisoj
  • 576
  • 6
  • 16
11
votes
1 answer

How implicit conversion works for non-type template parameters?

I guess (certain) implicit conversions apply when passing non-type template parameters. For example, there should be a conversion from int to std::size_t for expressions like std::array. However, consider the following code: template…
11
votes
4 answers

Is it possible to access values of non-type template parameters in specialized template class?

Is it possible to access values of non-type template parameters in specialized template class? If I have template class with specialization: template struct A { void f() { cout << major << endl; } } template…
stefanB
  • 77,323
  • 27
  • 116
  • 141
10
votes
1 answer

Specialization that is itself a template

I have a template class that I have some specializations for. But the next specialization is a template itself. How do you specify this: template class Action { public: void doStuff() { std::cout << "Generic\n"; } } // A…
Martin York
  • 257,169
  • 86
  • 333
  • 562
10
votes
1 answer

std class specialization - meeting the standard library requirements for the original std::array template

In Is it safe to define a specialization for std::array the questioner asks if it's safe to specialize std::array for a program defined type which has an expensive default constructor. The goal is to instead initialize all elements in the std::array…
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
10
votes
1 answer

resolution of c++template specification and overload

I've read the Why Not Specialize Function Templates and after experiment a little bit, I found an interesting thing. Here go the main.cxx: // main.cxx #include // Declarations /* template void foo(T); template<> void…
ls.
  • 395
  • 4
  • 13
10
votes
4 answers

Eliminating recursive template instantiation in C++

I want to define a macro that can be invoked in different places (at file scope) in order to create functions that do something. (In the example below the functions just print a message, but of course my real intent is to do some other useful…
Ari
  • 3,460
  • 3
  • 24
  • 31