如何在 C++11 模板中只接受数字和字符串?

Gia*_*nMS 3 c++ templates sfinae template-specialization c++11

正如 titlte 所说,在 C++11 中,如何声明一个仅接受数字(intlongfloatdouble和字符串的模板?

template<typename T>
class CustomClass {
    public:
        T data;
};
Run Code Online (Sandbox Code Playgroud)

Igo*_*nik 5

将其放在类定义中的任何位置:

static_assert(std::is_arithmetic<T>::value ||
              std::is_same<T, std::string>::value,
              "Wrong argument type");
Run Code Online (Sandbox Code Playgroud)

根据口味调整条件。