【发布时间】: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<T>::value 始终是 false。 SFINAE 好像不是这样工作的。
如果在 C++11 中无法实现此 trait,那么如何在 C++14 中实现 (std::is_final)?是否有一些新的语言功能可以实现这一点?
【问题讨论】:
标签: c++ templates c++11 c++14 sfinae