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

template specialization in another file c++. Which version gets

I have these files :- 1.h :- #include using namespace std; template void f() { cout<<"generic\n"; } 1.cpp :- #include "1.h" template <> void f () { cout<<"for ints only\n"; } main.cpp :- #include "1.h" int…
owagh
  • 3,428
  • 2
  • 31
  • 53
7
votes
1 answer

Can a template parameter's default argument be specialized?

In C++, if I have a template parameter, how can I cleanly specialize a default argument? For example, consider the following: template class Association; What if I want Value to instead default to float for class…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
7
votes
4 answers

Template typedef for std container (without specialization)?

Is it possible to use typedef on a std container without specializing it? Code like this works: typedef std::vector intVector; But for this code: template typedef std::vector DynamicArray; I get an error: template…
dtech
  • 47,916
  • 17
  • 112
  • 190
6
votes
3 answers

Why SFINAE gets messed up when changing the place of the class template specialization? Is this a C++ bug?

Following code gives compiler error which is expected (Demo): 1 template struct Range; 2 3 template > struct Unique; 4 template struct Unique 1)> > { typedef char…
iammilind
  • 68,093
  • 33
  • 169
  • 336
6
votes
3 answers

Template Specialization for basic POD only

Is there a subtle trick for template specialization so that I can apply one specialization to basic POD (when I say basic POD I don't particularly want struct POD (but I will take that)). template struct DoStuff { void operator()() {…
Martin York
  • 257,169
  • 86
  • 333
  • 562
6
votes
2 answers

inheriting from class of specialized self?

Is this valid C++? template class any_iterator : public any_iterator { public: typedef any_iterator any_iter_void; any_iterator() : any_iter_void() {} }; template<> class any_iterator { public: …
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
6
votes
4 answers

template specialization with multiple template parameters

Say I have this: template class foo { public: void set(const T &t); }; template void foo::set(const T &t) { int s = X; // ...etc } Could I specialize the function type 'T' but leave 'X' as a…
MarkP
  • 4,168
  • 10
  • 43
  • 84
6
votes
4 answers

How to specialize member of template class with template template parameter

I have a template class with an int and a template template parameter. Now I want to specialize a member function: template class Default{}; template class T = Default> struct Class { void member(); }; //…
Gabriel Schreiber
  • 2,166
  • 1
  • 20
  • 33
6
votes
2 answers

template specialization and rvalue reference, c++

I'm a bit confused about this little example: using mytype = std::vector; template void test(T item) { throw std::runtime_error(typeid(item).name()); } template<> void test(std::vector&& vec) { …
fjanisze
  • 1,234
  • 11
  • 21
6
votes
3 answers

Template specialization by another template (of same class)

I'm writing an array class. This array class can contain again arrays as members. When implementing a printing function, I need specializations. 26:template class array : public vector{ public: ... string* printToString(); …
6
votes
2 answers

Syntax for specializing function templates in C++

Suppose I have a function template where the type parameter is used as a return type only: template T foo() { return whatever; } Then what is the correct syntax to specialize that function template? Both of the following seem to…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6
votes
2 answers

Is this textbook wrong? Specialising some member functions but not others

I'm reading Vandevoorde and Josuttis's "C++ Templates The Complete Guide" (which seems pretty good, by the way). This claim (section 3.3) seems to be wrong and is not in the published errata: If you specialise a class template, you must also…
spraff
  • 32,570
  • 22
  • 121
  • 229
6
votes
1 answer

Template class specialization with different concepts gives redefinition error

I'm writing some code using std::hash and a Hashable concept. However I can't define specializations with two different concepts even if I don't have an ambiguous instance of them. #include #include #include…
ofo
  • 1,160
  • 10
  • 21
6
votes
1 answer

Access checking rules for template argument list in (particularly explicit) specializations

All standard references below refers to N4659: March 2017 post-Kona working draft/C++17 DIS. Consider the following class template A and class B which private defines a nested class C and enum class E: template class A…
dfrib
  • 70,367
  • 12
  • 127
  • 192
6
votes
4 answers

When is "explicit specialization" needed or desirable in C++?

I'm reading Primer C++ > Adventures in Functions > Templates > Explicit Specialization. To show the reason/use for Explicit Specialization, a case is illustrated: Consider a swap template function that can swap any type (int, double, struct…