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

template class specialization - how to pass the template argument to constructor?

I have the following code (simplified here for readability): enum id_t {B, C, D}; template class class_name; template class class_name { public: void member_func() { /* do something related…
user4546925
0
votes
1 answer

How to create compile-time templatized set/array/vector with fibonacci numbers using templates?

I have a class template template class A { static_assert(std::is_arithmetic::value, "U type must be arithmetic"); public: const std::set fibonacci = ???; //May be any structure with…
0
votes
1 answer

Template specialization using another class that implicit-converts to it

I don't know if this is possible, but I would like to understand better how this works. Can a class implict convertsion operation be used to match a template parameter? This is what I want to do. #include template struct…
Rangel Reale
  • 698
  • 6
  • 7
0
votes
2 answers

Partial specialization for destructor

I am just working on my school homework, and I am interested if C++ could create specialized destructor for pointers. I know that this is not good practice, but because of performance, I want to do it this way. Also because I am curious about it. So…
0
votes
1 answer

C++ template function resolution

I've got the following sample code and would like to get some help to understand why I am unable to compile it using clang and g++ on Linux? #include using namespace std; typedef enum COLORS { RED = 0, GREEN, BLUE, …
TheBadCat
  • 19
  • 1
  • 6
0
votes
2 answers

Template class with virtual member: linker error

Consider the following code. A is an abstract, generic class; B both implements and specializes it. This code seems trivially correct to me, but for some reason I end up with strange linker errors. template class A { public: …
0
votes
1 answer

How to specialize a type based on whether a template parameter has an alias

I want to be able to specialize a type based on whether a container has a specified typedef for example class SomethingElse {}; class Something { using Type = int; }; static constexpr bool value = ChooseIfType::value; Is there a way…
Curious
  • 20,870
  • 8
  • 61
  • 146
0
votes
1 answer

Is it possible for a specialized version to share some functionalities with the original template class?

Is it possible for a specialized version of a class to share some or all functionalities of the original template class? i.e. consider there is a template class, template class A { A() {} A(const A& ref) {} void…
Saif
  • 1,745
  • 5
  • 23
  • 46
0
votes
3 answers

specialization of member function of templated class

there: What is the result of the following codes? foo.h #include template struct Foo { void print() { std::cout << "foo\n"; } }; foo.cxx #include "foo.h" template<> void Foo::print() { std::cout <<…
0
votes
2 answers

Template Specialization of class method. "Function template already defined"

I've seen lots of SO questions about specialization in the context of methods, but not functions belonging to classes. I'm having a hard time translating the knowledge passed on from those questions to my problem here. I'm mucking around with a…
user819640
  • 250
  • 5
  • 14
0
votes
2 answers

Template specialization for classes which have a certain method

I have a templated method of a Lua State class, which tests if the object at a given index is of a given type: template bool is(int idx) { return luaL_testudata(L, idx, std::remove_pointer::type::name) != NULL; }; and a few…
Rena
  • 606
  • 7
  • 21
0
votes
1 answer

Specialization of templates with constraints in C++

I'm trying to implement managed->native converter in c++/cli. There are about 20 types to convert so I'm trying to use templates for this. The problem is that I should handle values types and reference types differently. Here is what I'm trying to…
0
votes
0 answers

Instantiating an overload of std::less outside of class that is being specialized

I am having a design problem with my code. I want to be able to an std::map with a key value pair of pcl::PointCloud and LidarFile(my own created class), but that requires the < operator to be overloaded since std::map uses comparisons, OR the…
0
votes
1 answer

Pass-by-reference template functions with `const char *` specialisation

I have a template function: template void foo(T const & t); And some specialisations of that function: template<> void foo(int const & t) { cout << t << endl; } template<> void foo(const char * const & t) { cout…
Tom
  • 7,269
  • 1
  • 42
  • 69
0
votes
1 answer

Why explicit instantiation in template specialization is giving me error?

Consider the code: ... template void Swap(T &,T &); template <> void Swap(structEmployee &,structEmployee &); int main() { template void Swap(char &,char &); short a=10,b=20; ... Swap(a,b); ... ... } It is giving…
dlp96
  • 131
  • 2
  • 10