FAQ Database Discussion Community


Partial template specialization and template-parameter list

c++,templates,language-lawyer,specialization
The N4296::14.5.5 [temp.class.spec] states that For partial specializations, the template argument list is explicitly written immediately following the class template name. For primary templates, this list is implicitly described by the template parameter list. There's also an example provided: template<class T1, class T2, int I> class A<T1, T2, I> {...

Specializations that are structurally identical

c++,templates,specialization
Consider the two partial specializations below: #include <iostream> #include <vector> #include <type_traits> template <typename, typename...> struct A; template <typename... Ts> struct A<int, Ts...> { void foo (int a) const {std::cout << a << '\n';} void operator()(const std::vector<int>& v) const {std::cout << v.size() << '\n';} }; template <typename... Ts> struct A<char,...

C++: template to check if expression compiles

c++,templates,sfinae,specialization,partial-specialization
When writing template specialization with SFINAE you often come to the point where you need to write a whole new specialization because of one small not-existing member or function. I would like to pack this selection into a small statement like orElse<T a,T b>. small example: template<typename T> int get(T&...

Template class specialization function call. C++

c++,templates,template-specialization,specialization
consider this code. template<class T> class A { public: void f(){..} void h(){..} }; template<> class A<int> { public: void f(){// something different..} //void h(){..} }; int main() { A<int> obj; obj.h(); // I want to call A<T>::h(), but compiler erred that there is no h function in A<int> }...

Overloading template by Return Type

c++,templates,overloading,return-type,specialization
Mooing Duck makes a comment here that "One function can't return multiple types. However, you can specialize or delegate to overloads, which works fine." I started thinking about that, and I'm trying to figure out, how is this legal code: template <typename T> T initialize(){ return T(13); } When called...

Partial template specialization may not work for functions, but isn't overloading the same thing?

c++,templates,function-overloading,specialization
is the lack of "partial template specialization" for functions actually a shortcoming? When I define a template, along with an overload to act as a specialization, it always seems to work. template<typename T> static T Func(T a) { std::cout << "a"; return a; } static long Func(long a) { std::cout...

How can a C++ template be specialized for all 32-bit POD types?

c++,templates,endianness,template-specialization,specialization
I've developed a simple template function for swapping the byte order of a single field: template <typename T> inline void SwapEndian(T& ptr) { char *bytes = reinterpret_cast<char*>(&ptr); int a = sizeof(T) / 2; while (a--) { char tmp = bytes[a]; int b = sizeof(T) - 1 - a; bytes[a] =...