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

How can this condition be put in template partial specialization?

template struct best_type { }; template struct best_type 8>> { // error: template argument 2 is invalid typedef byte type; }; The error is because of the…
0
votes
2 answers

How to make a friend of a partially specialized template class to another partially specialized template class?

I require a template specialised class to make friend with another template specialised class. Example: template class X { }; template class Y { }; Now I want to have only some specific specialisations of…
Aman Gupta
  • 81
  • 1
  • 1
  • 6
0
votes
1 answer

Using typedef inside template specializations

I'am newbie in c++, and i'm trying to write an additional template which contains type char or type int. After that I want to use this template in other template to select the type of data, depending of input data padding. template
Ilia
  • 3
  • 3
0
votes
0 answers

convoluted C++11 template specialization code: can it be written in a simpler way?

I'm using C++11 to develop a somewhat generic pipeline framework. For that, I defined: a DataBuffer class with reference counting. a Producer class which takes a certain input type "IN" and generate a certain output type "OUT" I want to make sure…
0
votes
2 answers

Can I have specialized template like here?

I want to have general class Vector of any dimensions with template type template class Vector { public: // constructors, methods and so on protected: T data[D]; }; and specializations for 2, 3 and 4 dimensions…
devalone
  • 535
  • 6
  • 21
0
votes
1 answer

Namespace constraint in template definition

I have to specialize a template function for all types in a given namespace rofl. Is a namespace constraint for a template type possible? I have the following in mind: template MyClass::MyFunc() { impl = T::create(); }
0
votes
1 answer

Template operator< not called

The following code implements template class Data whose purpose is to compare its member object. The intent is that if the member object is of the same type, that object's operator< is used for the comparison, and if the member objects are of…
StoneThrow
  • 5,314
  • 4
  • 44
  • 86
0
votes
3 answers

why this code has error with template specialization

I want to use one function pass parameters to different class, so I use class template specialization and variadic function template, bellow is the code: #include using namespace std; template class Package { …
0
votes
1 answer

How do I apply a different #define based on function template type in C++?

I am working with the HDF5 library and am trying to write a template function to support I/O with different data types. The goal just to reduce and simplify the copy and pasting i need to do to create all the different functions. In the HDF5 library…
crazywill32
  • 390
  • 1
  • 4
  • 12
0
votes
2 answers

what is the standard way to eliminate code redundancy with template class specialization?

I have a template class which recursively build fibonacci array using template recursion like this: #include #include "C++ Helpers/static_check.hpp" using namespace std; template struct valid_type :…
0
votes
4 answers

C++ specialized function template

I started using templates and I have a problem. I want to use function sum to the type char* and it fails to compile. Error I got: cannot initialize return object of type 'char*' with an rvalue of type 'int' Code: #include using…
Euro Pe
  • 73
  • 2
  • 8
0
votes
1 answer

How to elegantly restrict a template argument to be a `>`?

How to restrict a template argument of Wrapper to be a Wrapper> elegantly? Don't break content assist (Visual Studio). High readability. Not use a hacky approach. For some reasons, most solution love to hack. Make it obvious…
0
votes
1 answer

Why does this partial specialization of a template friend function work?

I am writing simple classes that implement vectors and matrices as part of trying to learn OpenGL. I have matrix and vector classes that look like this : // Forward declarations template struct vec; template
0
votes
2 answers

Proper way to specialise a method within a templated class

Context: I am currently trying to implement a std::vector like class. This is a school project: we need to write it like that, i.e. raw pointers and no STL-structure as std::vector or std::valarray. As I want this class to be as generic as…
Adrien Suau
  • 269
  • 3
  • 11
0
votes
1 answer

Full Template Specialization errors

Since I am SIMD'ifying my code I decided to use template specialization to handle each type case but it seems I did something wrong. template struct __declspec(align(16)) TVec2 { }; template<> __declspec(align(16)) struct TVec2 { …
James Nguyen
  • 1,079
  • 1
  • 10
  • 20