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

Partial specialisation of member function with non-type parameter

I have a template class with both a type and a non-type template parameter. I want to specialize a member function, what I finding is, as in the example below, I can do a full specialization fine. template struct foo { …
Eoin
  • 1,709
  • 11
  • 22
13
votes
3 answers

How to specialize only some members of a template class?

Code: template struct A { void f1() {}; void f2() {}; }; template<> struct A { void f2() {}; }; int main() { A data; data.f1(); data.f2(); }; ERROR: test.cpp: In function 'int main()': test.cpp:16: error: 'struct…
13
votes
2 answers

Partial template specialization of free functions - best practices

As most C++ programmers should know, partial template specialization of free functions is disallowed. For example, the following is illegal C++: template T mul(const T& x) { return x * N; } template T mul(const T&…
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
12
votes
6 answers

What are some other languages that support "partial specialization"?

Partial template specialization is one of the most important concepts for generic programming in C++. For example: to implement a generic swap function: template void swap(T &x, T &y) { const T tmp = x; y = x; x = tmp; } To…
12
votes
1 answer

A template that accepts only pointer type arguments

After seeing that a template can be partially specialized for reference or pointer types, I was wondering whether I can write a template that accepts only a pointer type to start with. This is my attempt: template struct…
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
12
votes
5 answers

C++ template partial specialization - specializing one member function only

Bumped into another templates problem: The problem: I want to partially specialize a container-class (foo) for the case that the objects are pointers, and i want to specialize only the delete-method. Should look like this: The lib code template…
Roman Pfneudl
  • 707
  • 1
  • 8
  • 21
11
votes
1 answer

Member definition of partially specialized classes

I'm trying to define member functions of partially specialized class templates, but different compilers have wildly different opinions of what I'm allowed to do and why. Let's take this slowly and start with something that works for all main…
11
votes
2 answers

Why does the C++ standard not allow function template partial specialization?

I read something that it might be confusing for the compiler to write template void calculator>::myMin(); but maybe just give it a hint like so? To make it clear that it is a partial specialization. template < , class…
Hakaishin
  • 2,550
  • 3
  • 27
  • 45
11
votes
6 answers

pointers as template parameters?

I have a container class, we'll call it template CVector { ... } I want to do something different with this class when T is a pointer type, e.g. something along the lines of: template CVector< SomeWrapperClass >; where…
D Garcia
  • 277
  • 1
  • 3
  • 7
10
votes
4 answers

Avoiding duplication of function definitions in template specializations

The class Widget has some functions that apply for all parameter types (common functions) and other functions that need to be specialized for given types (the uncommon functions). g++ insists that the specialization for Widget should also define…
Calaf
  • 10,113
  • 15
  • 57
  • 120
10
votes
3 answers

partial specialization ordering with non-deduced context

According to [temp.class.order] §14.5.5.2, the selection of a partial specialization of t in this example: template< typename > struct s { typedef void v, w; }; template< typename, typename = void > struct t {}; template< typename c > struct t< c,…
10
votes
1 answer

Partial Specialization of Alias Templates

Partial specializations of alias templates are not permitted: For example, trying to be creative, yields this error in clang: template using unwrapped_future_t = T; template using unwrapped_future_t> = typename…
10
votes
4 answers

specialize a member template without specializing its parent

I have a class template nested inside another template. Partially specializing it is easy: I just declare another template< … > block inside its parent. However, I need another partial specialization that happens to specify all its local template…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
10
votes
3 answers

Partial specialization for variadic template needs first non-variadic template parameter

The following code #include #include template struct Wrapper{ }; template struct is_wrapper : std::false_type {}; template struct is_wrapper> :…
levzettelin
  • 2,600
  • 19
  • 32
10
votes
3 answers

How to partially specialize a class template for all derived types?

I want to partially specialize an existing template that I cannot change (std::tr1::hash) for a base class and all derived classes. The reason is that I'm using the curiously-recurring template pattern for polymorphism, and the hash function is…
Doug
  • 8,780
  • 2
  • 27
  • 37
1
2
3
21 22