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

gcc vs clang behavior on partial specialization with variadic arguments plus extra argument of same type

The following code: #include template struct A { }; template struct A<0, T, args...> { }; int main () { A<0, int, 1> a0; …
Holt
  • 36,600
  • 7
  • 92
  • 139
9
votes
1 answer

C++ specialize a lambda for a certain type

I was playing around with the lambda "overloading" as presented here, and quickly came to the point where I'd find it convenient to create specialized lambda closures. So I was first trial-and-error'ing a bit, with my most promising trial being auto…
davidhigh
  • 14,652
  • 2
  • 44
  • 75
9
votes
1 answer

std::hash specialization using sfinae?

As an exercise I was trying to see if I could use SFINAE to create a std::hash specialization for std::pair and std::tuple when all of its template parameters are of an unsigned type. I have a little experience with them, but from what I understand…
Brian Rodriguez
  • 4,250
  • 1
  • 16
  • 37
9
votes
1 answer

What does SFINAE not work correctly with following has_member function?

I'm trying out examples from Walter Brown's TMP talk and I'm trying to get his has_member implementation working. However the implementation seems to falsely return true which leads me to believe there is some detail of SFINAE that I am not…
Nathan Doromal
  • 3,437
  • 2
  • 24
  • 25
9
votes
4 answers

Template function specialization for template class

Is it possible to write something like this in C++11/14? #include #include template T Get(); template struct Data { std::vector data; }; template <> template Data Get>()…
Kostya
  • 1,536
  • 1
  • 13
  • 22
9
votes
1 answer

Can I get an unspecialized vector type in C++?

A vector is specialized to reduce space consumption (1 bit for each element), but it's slower to access than vector. Sometimes I use a vector for performance reason, but if I convert a char to a bool, my compiler (Visual C++) may…
EFanZh
  • 2,357
  • 3
  • 30
  • 63
9
votes
3 answers

Is it safe to place definition of specialization of template member function (withOUT default body) in source file?

Here's what I mean: // test.h class cls { public: template< typename T > void f( T t ); }; - // test.cpp template<> void cls::f( const char* ) { } - // main.cpp int main() { cls c; double x = .0; c.f( x ); // gives EXPECTED…
9
votes
1 answer

Specializing std::hash to derived classes

I have an abstract base class Hashable that classes that can be hashed derive from. I would now like to extend std::hash to all classes that derive from Hashable. The following code is supposed to do exactly that. #include #include…
Flecto
  • 315
  • 2
  • 7
9
votes
3 answers

c++ template specialization for base class

I would like to have a special formatter for BASECLASS and all derived classes. I have the following classes: struct BASECLASS { ... }; struct SPECIALFORMAT : BASECLASS { ... } struct ANOTHERSPECIALFORMAT : BASECLASS { ... } template
Miquel
  • 8,339
  • 11
  • 59
  • 82
9
votes
2 answers

class template specialization that accepts all versions of const / volatile qualifications and & vs &&

I am specializing std::common_type for my type. I defined the following specialization: common_type And all is well. Then someone comes along and calls std::common_type. The default version acts the same if you…
9
votes
1 answer

Can I specialize std::begin and std::end for the return value of equal_range()?

The header provides std::equal_range(), as well as some containers having it as a member function. What bothers me with this function is that it returns a pair of iterators, making it tedious to iterate from the begin iterator to the end…
LB--
  • 2,506
  • 1
  • 38
  • 76
9
votes
3 answers

Can a single member of a class template be partially specialized?

I came across an interesting point that I wasn't able to explain or find an explanation for. Consider the following template definition (compiled with mingw g++ 4.6.2): template class Foo { public: void f(){} void…
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
9
votes
2 answers

Is it possible to use std::enable_if to select a member template specialization?

Given a class declaration class A { template T foo(); }; I would like to specialize A::foo for various types (int, ...) and type classes (POD, non-POD) of T. Unfortunately, I cannot seem to use std::enable_if for the latter. The…
Daniel Gehriger
  • 7,339
  • 2
  • 34
  • 55
9
votes
3 answers

How to test for presence of an inner class in a class via SFINAE?

I'm trying to have a different template specialization for classes which have an inner class with a particular name present. I've taken a clue from here and tried the following: #include template< typename T, typename Check = void >…
dragonroot
  • 5,653
  • 3
  • 38
  • 63
9
votes
2 answers

C++ Templates: Partial Template Specifications and Friend Classes

is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class template class X{ T t; }; Now you have partial specializations, for example, for pointers template…