如何确定模板专业化是否存在

How to decide if a template specialization exist

我想检查某个模板特化是否存在,一般情况没有定义。

鉴于:

template <typename T> struct A; // general definition not defined
template <> struct A<int> {};   // specialization defined for int

我想定义一个这样的结构:

template <typename T>
struct IsDefined
{
    static const bool value = ???; // true if A<T> exist, false if it does not
};

有没有办法做到这一点(最好不用 C++11)?

谢谢

利用无法将 sizeof 应用于不完整类型的事实:

template <class T, std::size_t = sizeof(T)>
std::true_type is_complete_impl(T *);

std::false_type is_complete_impl(...);

template <class T>
using is_complete = decltype(is_complete_impl(std::declval<T*>()));

See it live on Coliru


这是一个有点笨拙但有效的 C++03 解决方案:

template <class T>
char is_complete_impl(char (*)[sizeof(T)]);

template <class>
char (&is_complete_impl(...))[2];

template <class T>
struct is_complete {
    enum { value = sizeof(is_complete_impl<T>(0)) == sizeof(char) };
};

See it live on Coliru

这是一个替代实现,始终使用@Quentin 使用的相同技巧


C++11 版本

template<class First, std::size_t>
using first_t = First;

template<class T>
struct is_complete_type: std::false_type {};

template<class T>
struct is_complete_type<first_t<T, sizeof(T)>> : std::true_type {};

Example on wandbox


暂定的 C++03 版本不起作用

template<typename First, std::size_t>
struct first { typedef First type; };

template<typename T>
struct is_complete_type { static const bool value = false; };

template<typename T>
struct is_complete_type< typename first<T, sizeof(T)>::type > { static const bool value = true; };

本例中的错误是

prog.cc:11:8: error: template parameters not deducible in partial specialization: struct is_complete_type< typename first::type > { static const bool value = true; }; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

prog.cc:11:8: note: 'T'