In specializing a class template, I would like to have one specialization target full-blown classes (complete with constructor, destructor, etc.) and one specialization target primitives (int
, float
, etc.). The only partial specialization I've seen is with targeting pointer types (via T*
). Is there a way to do this?
Asked
Active
Viewed 1,647 times
3

TheBuzzSaw
- 8,648
- 5
- 39
- 58
-
What is special about the full-blown classes that you want to specialize differently? Do they have some special member that you need? – Vaughn Cato Jul 02 '12 at 01:50
-
Well, for primitives, I can do a blanket `memcpy` for certain operations, whereas for classes, I need to invoke each copy operation individually. – TheBuzzSaw Jul 02 '12 at 02:03
-
1@TheBuzzSaw: Then you don't want to specialize on primitive/classes, but rather on POD vs non-POD types – David Rodríguez - dribeas Jul 02 '12 at 03:51
-
You probably want to specialize based on something like the type trait is_trivially_copyable – bames53 Jul 02 '12 at 04:20
-
@DavidRodríguez-dribeas: And in C++11, the decided factor is `is_standard_layout`. Standard layout classes can be treated as a sequence of bytes, POD being a subset of those. – GManNickG Jul 02 '12 at 05:37
2 Answers
7
You can used C++11 type_traits. Here is something to get you started, you can specialize more as needed:
#include <type_traits>
#include <iostream>
template<typename T, typename E = void>
struct A; // undefined
template<typename T>
struct A<T, typename std::enable_if<std::is_class<T>::value && !std::is_pod<T>::value>::type> {
A() { std::cout << "I'm a class, but I'm not a pod type" << std::endl; }
};
template<typename T>
struct A<T, typename std::enable_if<std::is_class<T>::value && std::is_pod<T>::value>::type> {
A() { std::cout << "I'm a class and a pod type" << std::endl; }
};
template<typename T>
struct A<T, typename std::enable_if<!std::is_class<T>::value>::type> {
A() { std::cout << "I'm not a class" << std::endl; }
};
class X {};
class Y { ~Y(){} };
int main()
{
A<X> a1;
A<Y> a2;
A<int> a3;
}

Jesse Good
- 50,901
- 14
- 124
- 166
0
boost::has_trivial_assign
should give you this info.
template <class T>
struct has_trivial_assign : public true_type-or-false_type {};

Tom Kerr
- 10,444
- 2
- 30
- 46
-
Classes can have trivial assignment operators, so this would give the OP false positives. – underscore_d Dec 28 '17 at 14:25