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
6
votes
4 answers

Why don't I have to define the same members when I do total specialization of a class template in C++?

I'm very surprised to find that the following compiles: #include using namespace std; template class SomeCls { public: void UseT(T t) { cout << "UseT" << endl; } }; template<> class SomeCls { // No UseT?…
allyourcode
  • 21,871
  • 18
  • 78
  • 106
6
votes
3 answers

How to specialize a template class method for a specific type?

I have codes like this: class Bar { public: void print() { std::cout << "bar\n"; } }; template class Foo { public: template ::value,T>::type> void print() { t.print(); …
user2269707
6
votes
2 answers

Template class specialization

I did read some of the related threads but still the issue was not clear: #include #include #include template <> class stack { public: std :: vector stackVector; }; The compilation…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
6
votes
1 answer

One template specialization for several enum values

Normally if I want to have a templated (data) class by enum I would write something like this enum class Modes : int { m1 = 1, m2 = 2, m3 = 3 }; template class DataHolder { }; template<> class DataHolder { …
Croolman
  • 1,103
  • 13
  • 40
6
votes
1 answer

What's the syntax to partially specialize a parmeter pack argument for void type?

I can't find a way to get this to work. Is it even possible? I don't see why it wouldn't be. template struct FieldTypeById { using Type = int; }; template struct…
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
6
votes
4 answers

C++ : using index as template parameter in for loop

given following templates and specialization enum CountryName { Armenia = 0 , Georgia, Size = 2 }; template class CountryInfo; template <> class CountryInfo { /* CODE HERE */ }; template <> class…
libxelar.so
  • 473
  • 4
  • 16
6
votes
2 answers

C++ generic way to define multiple functions with a template

In C++ is it possible to define multiple methods based of the number of template parameters provided? Similar to how variadic functions work? With functions I can do template struct VariadicFunctionCallback { typedef…
6
votes
1 answer

C++ specialized template function receiving literal string

I am writting a template method which has many specializations. class FieldValue { public: ... template< typename T > void Set( const T& value ); ... }; One of them is: template <> void FieldValue::Set< const char* >( const char* const&…
6
votes
2 answers

specialize only (a part of) one method of a template class

If I have a template class template class C { public: void method1() { ... } void method2() { ... } std::string method3(T &t) { // ... std::string s = t.SerializeToString(); // ... return s; …
fferri
  • 18,285
  • 5
  • 46
  • 95
6
votes
3 answers

Partial class template specialisation for multiple types

I have a class which allows for a vector to be created holding any type or class. However I'd like to add additional functionality for numerical types. template <> class Vec : public VecBase { // == METHODS == public: …
6
votes
1 answer

"Type is incomplete" (but isn't) and code compiles

I have a template class template class StateMachine; and a specialization of it template
Timo
  • 9,269
  • 2
  • 28
  • 58
6
votes
3 answers

C# specialize generic class

Is it possible to do the following specialization in C#? I can do this in C++ but do not understand how to achieve the same result in C#. class GenericPrinter { public void Print() { Console.WriteLine("Unspecialized method"); …
Nosturion
  • 289
  • 3
  • 12
6
votes
1 answer

member template variable specializing

A class can contain a member template variable which must be static: class B { public: template static X var; B() { std::cout << "Create B " << __PRETTY_FUNCTION__ << std::endl; } template…
Klaus
  • 24,205
  • 7
  • 58
  • 113
6
votes
1 answer

How to deduce template parameter in explicit specialization of function template?

Consider following case: #include template void f(T) { std::cout << "#1\n"; } // #1 template <> void f(const int) { std::cout << "#2\n"; } // #2 int main() { f(1); // call #1 f(1); // call #2 …
Carousel
  • 728
  • 4
  • 13
6
votes
1 answer

Specialization of inherited nested template class

The following source code is brought from: Understanding partial specialization of inherited nested class templates #include struct Base { template struct Inner: std::true_type {}; template