使用 std::is_detected_exact 检测 operator++ 签名
Detect operator++ signature with std::is_detected_exact
我想在编译时检测给定类型是否具有带有库基础知识 TS v2 type_traits' is_detected_exact 助手的预递增运算符 - 但是,我似乎误解了这个helper 或我提供了错误的参数,以下代码无法编译:
#include <experimental/type_traits>
template<typename T>
using operator_plusplus_t = decltype(&T::operator++);
template<typename T>
using has_pre_increment = std::experimental::is_detected_exact<T&, operator_plusplus_t, T>;
struct incrementer
{
incrementer& operator++() { return *this; };
};
static_assert(has_pre_increment<incrementer>::value, "type does not have pre increment");
我得到的错误是这个错误(static_assert 失败):
<source>:14:15: error: static assertion failed: type does not have pre increment
static_assert(has_pre_increment<incrementer>::value, "type does not have pre increment");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1
我期待这段代码能够编译,因为 "incrementer" 结构有一个没有参数的 operator++ 方法返回对其类型的引用...
也许你能给我指明正确的方向,在此先感谢!
您可以改用 decltype(++std::declval<T>())
。
我想在编译时检测给定类型是否具有带有库基础知识 TS v2 type_traits' is_detected_exact 助手的预递增运算符 - 但是,我似乎误解了这个helper 或我提供了错误的参数,以下代码无法编译:
#include <experimental/type_traits>
template<typename T>
using operator_plusplus_t = decltype(&T::operator++);
template<typename T>
using has_pre_increment = std::experimental::is_detected_exact<T&, operator_plusplus_t, T>;
struct incrementer
{
incrementer& operator++() { return *this; };
};
static_assert(has_pre_increment<incrementer>::value, "type does not have pre increment");
我得到的错误是这个错误(static_assert 失败):
<source>:14:15: error: static assertion failed: type does not have pre increment
static_assert(has_pre_increment<incrementer>::value, "type does not have pre increment");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Compiler returned: 1
我期待这段代码能够编译,因为 "incrementer" 结构有一个没有参数的 operator++ 方法返回对其类型的引用...
也许你能给我指明正确的方向,在此先感谢!
您可以改用 decltype(++std::declval<T>())
。