Questions tagged [partial-specialization]

Partial template specialization is a particular form of class template specialization. Usually used in reference to the C++ programming language, it allows the programmer to specialize only some arguments of a class template, as opposed to explicit specialization, where all the template arguments are provided.

316 questions
117
votes
7 answers

C++ function template partial specialization?

I know that the below code is a partial specialization of a class: template class MyClass { … }; // partial specialization: both template parameters have same type template class MyClass { ……
Narek
  • 38,779
  • 79
  • 233
  • 389
98
votes
8 answers

How to do template specialization in C#

How would you do specialization in C#? I'll pose a problem. You have a template type, you have no idea what it is. But you do know if it's derived from XYZ you want to call .alternativeFunc(). A great way is to call a specialized function or class…
user34537
93
votes
4 answers

Why function template cannot be partially specialized?

I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template void f() {} //allowed! template<> void f
53
votes
4 answers

Template partial specialization for integral non-type parameters and non-integral non-types, difference between g++ and clang

The following is a simple template partial specialization: // #1 template struct foo { static const char* scenario() { return "#1 the base template"; } }; // #2 // partial specialization where T is unknown…
51
votes
4 answers

Tag dispatch versus static methods on partially specialised classes

Suppose I want to write a generic function void f(), which does one thing if T is a POD type and another thing if T is non-POD (or any other arbitrary predicate). One way to achieve this would be to use a tag-dispatch pattern like the standard…
44
votes
5 answers

"invalid use of incomplete type" error with partial template specialization

The following code: template struct foo { void bar(); }; template void foo ::bar() { } gives me the error invalid use of incomplete type 'struct foo' declaration of 'struct foo
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
42
votes
2 answers

Why is it disallowed for partial specialization in a non-type argument to use nested template parameters

I have this code template struct A; template struct A { /* ... */ }; // should work A<25> a; That is, for numbers N that are divisible by 5, the compiler should use the partial specialization. But the…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
34
votes
4 answers

(Partially) specializing a non-type template parameter of dependent type

Maybe I'm tired, but I'm stuck with this simple partial specialization, which doesn't work because non-type template argument specializes a template parameter with dependent type 'T': template struct X; template
iavr
  • 7,547
  • 1
  • 18
  • 53
32
votes
0 answers

What does six dots mean in variadic templates?

The following are some partial specializations for std::is_function from libstdc++'s : /// is_function template struct is_function : public false_type { }; template
chys
  • 1,546
  • 13
  • 17
23
votes
2 answers

Multiple SFINAE class template specialisations using void_t

Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member…
22
votes
2 answers

What are the 6 dots in template parameter packs?

While looking at this question I found myself in the cpp reference site where I noticed a strange and new to me syntax : template struct is_function : std::true_type {}; Yep, 6 dots ! Initially…
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
20
votes
2 answers

Partial specialization of function templates

Does anyone know whether, in C++11, function templates can be partially specialized?
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
20
votes
2 answers

Get the signed/unsigned variant of an integer template parameter without explicit traits

I am looking to define a template class whose template parameter will always be an integer type. The class will contain two members, one of type T, and the other as the unsigned variant of type T -- i.e. if T == int, then T_Unsigned == unsigned int.…
Blair Holloway
  • 15,969
  • 2
  • 29
  • 28
14
votes
1 answer

Can template partial specialization narrow the argument type in C++?

In the next program, struct template A has a specialization A: template struct A { constexpr operator int() { return 1; } }; template struct A { constexpr operator int() { return 2; } }; int main() { static_assert(…
Fedor
  • 17,146
  • 13
  • 40
  • 131
13
votes
1 answer

Partial template specialization ambiguity

I cant see why the statement in main is ambiguous. template struct X { void f() { cout << "Primary template" << endl; } }; template struct X {void f() { cout << "Partial specialization 1" <<…
1
2 3
21 22