小编Duc*_*van的帖子

继承和is_detected_v提供了一个奇怪的结果(C++ 17)

我有简化的代码版本:

#include <experimental/type_traits>

template<class T> using has_data_t = decltype(T::data());

template <class B> constexpr auto get_data() {
    return std::experimental::is_detected_v<has_data_t, B>;
}

template <typename Topt> struct opt_base {
    static constexpr bool i = get_data<Topt>();
  //static constexpr auto j = get_data<Topt>(); // fail to compile
};

struct opt : public opt_base<opt> {
    static int data() { return 7;}
};

int main() {
    static_assert(std::experimental::is_detected_v<has_data_t, opt>);
}
Run Code Online (Sandbox Code Playgroud)

这段代码编译.但是,如果您取消注释注释行,则断言失败.它用GCC 7.1和Clang 4.0.0进行了测试.编译参数:-std = c ++ 1z -O3 -Wall. 演示

c++ templates c++17

6
推荐指数
1
解决办法
248
查看次数

标签 统计

c++ ×1

c++17 ×1

templates ×1