是否可以根据type_traits信息重载函数/类模板?
示例:
#include <type_traits>
template<typename Object>
class object_worker
{
public:
object_worker(Object&& o) // o - is not POD
{
// do something
}
};
template<typename Object>
class object_worker<std::is_pod<Object>::value == true> // how to make this thing work?
{
public:
object_worker(Object &&o) // o - is POD
{
// do something different
}
};
答案 0 :(得分:1)
是的,你可以做这样的事情。它被广泛使用。
template<typename T, bool = is_pod<T>::value>>
class foo
{
};
// This is a partial template specialization.
// Triggered only when is_pod<T>::value is true
template<typename T>
class foo<T, true> // T can be only a POD type
{
};