0xb*_*00d 3 c++ templates type-traits enable-if c++17
假设我有一个foo带有模板参数的类,T我想为对应于的引用和常量引用类型提供一个 using 声明T:
template<typename T>
struct foo
{
using reference = T&;
using const_reference = T const&;
};
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以使用declarations来“启用”这些,如果T不是void 没有专门化整个班级foo?
您可以从具有以下特化的基类继承void:
template<typename T>
struct typedefs {
using reference = T&;
using const_reference = T const&;
};
template<>
struct typedefs<void> {};
template<typename T>
struct foo : typedefs<T>
{};
Run Code Online (Sandbox Code Playgroud)