我想按类型组以及某些特定的简单类型的额外定义来专门化模板。是否在C++ 11中可以提升1.60?以下伪代码说明了我的意图:

template <typename T> // Universal definition
struct convert
{  ...  }

template <> /* Definition for integral types like defined by std::type_traits */
struct convert<integral_types>
{  ...  }

template <> /* Definition for floating point types like defined by type_traits */
struct convert<floating_types>
{  ...  }

template <> /* Exception from integral types - specific definition */
struct convert<char>
{  ...  }

我认为可以通过标签分派(dispatch)器解决此问题,但是我不确定这是否是最佳解决方案。另一个选项是enable_if,与is_integral(和类似的组)结合使用,但是简单的char类型是有问题的...

最佳答案

您可以执行以下操作:

template <typename T, typename Enabler = void> // Universal definition
struct convert
{  ...  };

template <typename T> /* Definition for integral types like defined by std::type_traits */
struct convert<T, std::enable_if_t<std::is_integral<T>::value>>
{  ...  };

template <> /* Exception from integral types - specific definition */
struct convert<char, void>
{  ...  };

09-15 22:43