【问题标题】:Traits for detecting final classes [duplicate]检测最终类的特征[重复]
【发布时间】:2016-07-17 12:26:40
【问题描述】:

是否可以在编译时确定类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&lt;T&gt;::value 始终是 false。 SFINAE 好像不是这样工作的。

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

【问题讨论】:

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


    【解决方案1】:

    根据此处的信息,在此答案之后,问题发生了根本性的变化。我不会追逐问题的演变。

    只需使用std::is_final

               

    【讨论】:

    • 这是如何实现的?它需要语言/编译器支持吗?
    • @ZizhengTai:是的,它是编译器提供的魔法,或者使用编译器特定的方式实现。
    • 嗯...这很奇怪:-/
    • @ZizhengTai : 在&lt;type_traits&gt; 的领域里不是... ;-]
    猜你喜欢
    • 2018-09-27
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2019-10-25
    相关资源
    最近更新 更多