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

How to check if a function template has been specialized?

Is there a way to establish at compile time if a certain function template was specialized? For example, assume the following function template: template void foo(); I want to test if foo<42> was specialized. Note that the declaration…
7
votes
2 answers

How to prevent instantiation of a C++ template class method when a specific condition is met?

I'm currently writing a generic vector template class (the geometric entity, not the container) with the following signature... template< typename T, unsigned N > class vector {...} ... where T is an arithmetic type and N, the dimension. I…
pmjobin
  • 813
  • 1
  • 7
  • 15
7
votes
2 answers

Error: class template partial specialization contains a template parameter that cannot be deduced

I'd appreciate help figuring out what going on in this problem that's come up in my code which I've reduced to the following: typedef unsigned short ushort; template struct Foo { }; // Specialization -- works when not a…
Olumide
  • 5,397
  • 10
  • 55
  • 104
7
votes
2 answers

How-to specialize template method in subclass(c++)?

I'm trying to specialize a template method of non-template class in its subclass: // .h file class MyWriter { public: template void test(const T & val) { std::cout << val << "\n"; } }; // .cpp file class MyType…
Dmitriy
  • 5,357
  • 8
  • 45
  • 57
7
votes
1 answer

Specializing Template Constructor Of Template Class

My brain has melted due to several weeks of 14-hour days. I have a template class, and I'm trying to write a template convert constructor for this class, and specialize that constructor. The compiler (MSVC9) is quite displeased with me. This is…
John Dibling
  • 99,718
  • 31
  • 186
  • 324
7
votes
1 answer

Template class specialization with template class

Related questions: c++ nested template specialization with template class template class specialization with template class parameter Consider the following code: template struct is_std_vector: std::false_type { }; …
7
votes
1 answer

Overload resolution and explicit template arguments

The following code prints "func 2". Why does the compiler treat the second template as a better match, in presence of explicit (not deduced) template arguments? Why is there no ambiguity? I'd appreciate quotations from the C++ standard. #include…
Igor R.
  • 14,716
  • 2
  • 49
  • 83
7
votes
3 answers

C++ constructor template specialization

I'm trying to create a specialized constructor for std::string arguments, but the other one is always used when I call it with a string argument. struct Literal : Expression { template Literal(V val) { value =…
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
7
votes
1 answer

Why does using the scope resolution operator change which overloaded template in the global namespace gets called?

Consider the following code: #include template void foo(W& a, T& t) { std::cout << "generic" << std::endl; } template