c++ - 检测最终类别的特征

标签 c++ templates c++11 c++14 sfinae

<分区>

是否有可能确定一个类T标记为 final在编译时?我一直在尝试修改 this answer的做法:

template<typename T>
struct sub {
    using type = struct : T {};
};

template<typename T>
struct is_final {
    using yes = char;
    using no = struct { char arr[2]; };

    template<typename U> static yes test(...);
    template<typename U> static no  test(typename sub<U>::type*);

public:
    static constexpr bool value = sizeof (test<T>(nullptr)) == sizeof (yes);
};

但它不起作用; is_final<T>::value总是 false .似乎 SFINAE 不是那样工作的。

如果无法在 C++11 中实现此特性,那么在 C++14 中如何实现(std::is_final)?是否有一些新的语言功能可以实现这一点?

最佳答案

根据此处的信息,在此答案之后,问题发生了根本性的改变。我不打算追问这个问题的演变。

只需使用 std::is_final .

关于c++ - 检测最终类别的特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38421162/

相关文章:

c++ - 如何通过专门化在具有单独声明和定义的模板化类的方法上使用 std::enable_if

c++ - 如何检查所需的输入数量是否正确

c++ - 为什么 std::forward 将我的左值变成右值?

c++ - 需要有关 C++ 中二维数组动态内存分配的帮助

c++ - SWIG 生成了具有任意数量参数的 TCL 包装器

c++ - 检测(可能是抽象的)基类的 protected 构造函数

c++ - 两个候选函数模板。使一个参数成为引用后,选择不太专业的模板

c++ - 错误 : (E112) get interface failed: port is not bound - SystemC

c++ - 在模板类内 : Use a class method as a template argument for another class method

c++ - 在二进制模式下使用格式化 IO 操作?