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

Member access and template specialization

I have this class template template class Wrapper { public: virtual void parse(std::string s) = 0; protected: T value; }; ideally, each type should know how to parse itself from a string, so I would like to…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
8
votes
1 answer

Template specialization when parameter values are equal

I have a function of the form template void f(); That I'd like to specialize when a == b. Pseudocode looks something like: template void f(){ //something} template void f(){ //something…
Ben Jones
  • 919
  • 1
  • 8
  • 22
8
votes
1 answer

Detect if two types are a specialization of a same class template?

I would like to know how to write a type_traits class to detect whether two types are specializations of the same template class. The big problem is that it should work for mixed types/non-types template class like: template
Vincent
  • 57,703
  • 61
  • 205
  • 388
8
votes
1 answer

template metafunction for detecting template specialisations

Inspired by this question, i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations: template class Templ... typedef Templ stringInstance; typedef Templ
8
votes
2 answers

Specializing C++ template based on presence/absense of a class member?

Consider the following: struct A { typedef int foo; }; struct B {}; template struct C {}; I want to specialize C so that C gets one specialization and C gets the other, based on the presence or absence…
drwowe
  • 1,975
  • 1
  • 15
  • 17
7
votes
3 answers

Mixing partial template specialization and default template parameters

I would like to create a generic vector class and create specializations for a few cases. Something like this (it does not compile, but hopefully communicates my intentions): template class Vector { public: typedef…
Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
7
votes
1 answer

Clang claims this (partially specialized) template instantiation is ambiguous. Is it right?

From clang 9.0.0 onwards, all versions of clang reject the following code when compiled with -std=c++17 (or -std=c++20): #include template struct A; // #1 template < template class Holder ,…
Caninonos
  • 1,214
  • 7
  • 12
7
votes
1 answer

partial template specialization

I have a scenario in which there is a template class template class Foo { typedef Y::NestedType Bar; int A (Bar thing); void B(); int C(X that); // other stuff }; and then I would like the A() method to have a…
7
votes
0 answers

GCC/Clang function attributes per template instantiation

I have some hand-vectorized C++ code that I'm trying to make a distribute-able binary for via function multiversioning. Since the code uses SIMD intrinsics for different instruction sets (SSE2, AVX2, AVX512), it uses template specializations to…
7
votes
2 answers

Code duplication and template specialization (when the specialized function has different return types)

I am creating a templated class D, with a method (operator(), in this case) that returns different types, depending on the value of N. I could only make this work by creating two separate class declarations, but this came at the cost of a lot of…
hugomg
  • 68,213
  • 24
  • 160
  • 246
7
votes
2 answers

Specialize template of templated data type

I have this situation: #include template T f() { return T(); } template<> template std::vector f>() { return { T() }; } int main(){ f>(); } I'm trying to specialize the…
João Paulo
  • 6,300
  • 4
  • 51
  • 80
7
votes
2 answers

Add member functions and member variables based on template argument

I have a family of functions {f_n} where f_0 is continuous, f_1 is continuously differentiable, $f_{n} \in C^{n}[a,b]$ so on. I have a C++ class which gives a numerical evaluation of f_n via a lookup table on a vector v template
user14717
  • 4,757
  • 2
  • 44
  • 68
7
votes
2 answers

How does template specialization with integer types work?

I have a template function with a single parameter , and I would like to make specializations of this function for different integral types. This seemed obvious at first, however after few attempts I found that I do not really understand how the…
Pasha
  • 6,298
  • 2
  • 22
  • 34
7
votes
3 answers

Specializing a template method with enable_if

I'm writing a template class that stores a std::function in order to call it later. Here is the simplifed code: template struct Test { void call(T type) { function(type); } std::function
Mark Morrisson
  • 2,543
  • 4
  • 19
  • 25
7
votes
3 answers

C++ template partial specialization - Most specialized with unique_ptr

I am trying to create partially specialized template, and have specialize even further if passed a std::unique_ptr template struct Foo; // A template struct Foo, typename…
Arelius
  • 1,216
  • 8
  • 15