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
10
votes
3 answers

Return different types with different template parameter value (but same type)

What I want to do is to define 3 functions like these: template int test() { return 8; } template float test() { return 8.8; } template std::string test() { return "8.9"; } int main() { int a = test<0>(); …
陈浩南
  • 633
  • 4
  • 12
10
votes
1 answer

g++ and clang++ different behaviour when `std::make_index_sequence` and `std::index_sequence` are used for a template parameter default type

Another "who's right between g++ and clang++?" question for C++ standard gurus. Given the following code #include template > struct foo; template
10
votes
3 answers

C++ Using pointers to template objects

I have a class named ABC which has a class template: template class ABC{} In another class I am trying to store of objects ABC in a list: class CDE{ private: list some_list; } I intend to store objects of ABC which might have…
cyrux
  • 233
  • 5
  • 15
10
votes
4 answers

Why does decltype(declval().func()) work where decltype(&T::func) doesn't?

I was trying to detect the presence of a member function baz() in a template parameter: template struct ImplementsBaz : public std::false_type { }; template struct ImplementsBaz :…
jtbandes
  • 115,675
  • 35
  • 233
  • 266
10
votes
4 answers

C++ template partial specialization: Why cant I match the last type in variadic-template?

I try to write a IsLast type traits to check if a given type is the last one in a std::tuple, but the code below does not compile. I know how to get around it but I am curious why the compiler does not like it.I guess there must be some rule on…
10
votes
2 answers

decltype for overloaded member function

I have this code: struct Foo { int print(int a, double b); int print(int a); void print(); void print(int a, int b, int c); void other(); }; I can call decltype(&Foo::other) but calling decltype(&Foo::print) end with error,…
10
votes
2 answers

Should the following program compile according to standard?

After my discovery of incosistency between MSVC and GCC (probably clang too) in compilation and linking of the same code, I've become curious should this program actually compile and link and thus it's bug in MSVC (which reports a linker error) or…
Predelnik
  • 5,066
  • 2
  • 24
  • 36
10
votes
4 answers

How to work around partial specialization of function template?

For example, I have a class: class A { enum {N = 5}; double mVariable; template void f(T& t) { g(mVariable); // call some function using mVariable. f(t); // go to next loop } …
user1899020
  • 13,167
  • 21
  • 79
  • 154
10
votes
1 answer

Partial Specialization of Alias Templates

Partial specializations of alias templates are not permitted: For example, trying to be creative, yields this error in clang: template using unwrapped_future_t = T; template using unwrapped_future_t> = typename…
10
votes
1 answer

Difference between T[N] and T[] in template specializations?

Looking at the example implementation of std::is_array, they have the following code: template struct is_array : std::true_type {}; template struct is_array : std::true_type {}; When will the T[]…
uk4321
  • 1,028
  • 8
  • 18
10
votes
4 answers

Template specialization for enum

Is it possible to specialize a templatized method for enums? Something like (the invalid code below): template void f(T value); template <> void f(T value); In the case it's not possible, then supposing I have specializations…
nilton
  • 798
  • 1
  • 8
  • 11
10
votes
3 answers

How to extract the highest-indexed specialization from a structure?

I'm trying to do some template metaprogramming and I'm finding the need to "extract" the highest index of a specialization of some structure in some type. For example, if I have some types: struct A { template struct D; …
user541686
  • 205,094
  • 128
  • 528
  • 886
10
votes
2 answers

Specializating a function template that takes a universal reference parameter

How do I specialize a function template that takes a universal reference parameter? foo.hpp: template void foo(T && t) // universal reference parameter foo.cpp template<> void foo(Class && class) { // do something…
David Stone
  • 26,872
  • 14
  • 68
  • 84
10
votes
3 answers

Why compiler doesn't give error while defining similar template specializations?

What is the procedure of comparing the class template specializations? The standard is not detailed on this point (or I am missing the right place). My question has NOTHING TO DO with deciding what specialization to use during the instantiation.…
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
9
votes
5 answers

Do template specializations require template<> syntax?

I have a visitor class resembling this: struct Visitor { template void operator()(T t) { ... } void operator()(bool b) { ... } }; Clearly, operator()(bool b) is intended to be a…
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180