std::declval<T>()、SFINAE 和删除的构造函数
std::declval<T>(), SFINAE and deleted constructor
在 C++11 中使用 SFINAE 进行方法检测,我写了这个小 运行 示例:
#include <type_traits>
struct Foo
{
Foo();// = delete;
Foo(int);
void my_method();
};
template <typename T, typename ENABLE = void>
struct Detect_My_Method
: std::false_type
{
};
template <typename T>
struct Detect_My_Method<T, decltype(T().my_method())>
: std::true_type
{
};
int main()
{
static_assert(!Detect_My_Method<double>::value, "");
static_assert(Detect_My_Method<Foo>::value, "");
}
按预期工作。
但是,如果我删除 Foo 的空构造函数:
struct Foo
{
Foo() = delete;
Foo(int);
void my_method();
};
示例 不再工作 ,我收到此错误消息:
g++ -std=c++11 declVal.cpp
declVal.cpp: In function ‘int main()’:
declVal.cpp:33:3: error: static assertion failed
static_assert(Detect_My_Method<Foo>::value, "");
问题:解释及如何解决?
空构造函数被删除时构造:
decltype(Foo().my_method());
不再是无效并且编译器立即抱怨
error: use of deleted function ‘Foo::Foo()’
一种解决方案是使用 std::decval<T>()
Converts any type T to a reference type, making it possible to use
member functions in decltype expressions without the need to go
through constructors.
因此替换:
template <typename T>
struct Detect_My_Method<T, decltype(T().my_method())>
: std::true_type
{
};
来自
template <typename T>
struct Detect_My_Method<T, decltype(std::declval<T>().my_method())>
: std::true_type
{
};
解决问题。
经验教训:
decltype(Foo().my_method()); // invalid
decltype(std::declval<Foo>().my_method()); // fine
不等价。
此外,还有另一种定义测试的方法,既不需要指向对象的引用或指针,也不需要函数的特定签名:
template<class T>
typename std::is_member_function_pointer<decltype(&T::my_method)>::type test_member_function_my_method(int);
template<class T>
std::false_type test_member_function_my_method(...);
template<class T>
using has_member_function_my_method = decltype(test_member_function_my_method<T>(0));
用法:
static_assert(!has_member_function_my_method<double>::value, "");
static_assert(has_member_function_my_method<Foo>::value, "");
在 C++11 中使用 SFINAE 进行方法检测,我写了这个小 运行 示例:
#include <type_traits>
struct Foo
{
Foo();// = delete;
Foo(int);
void my_method();
};
template <typename T, typename ENABLE = void>
struct Detect_My_Method
: std::false_type
{
};
template <typename T>
struct Detect_My_Method<T, decltype(T().my_method())>
: std::true_type
{
};
int main()
{
static_assert(!Detect_My_Method<double>::value, "");
static_assert(Detect_My_Method<Foo>::value, "");
}
按预期工作。
但是,如果我删除 Foo 的空构造函数:
struct Foo
{
Foo() = delete;
Foo(int);
void my_method();
};
示例 不再工作 ,我收到此错误消息:
g++ -std=c++11 declVal.cpp
declVal.cpp: In function ‘int main()’:
declVal.cpp:33:3: error: static assertion failed
static_assert(Detect_My_Method<Foo>::value, "");
问题:解释及如何解决?
空构造函数被删除时构造:
decltype(Foo().my_method());
不再是无效并且编译器立即抱怨
error: use of deleted function ‘Foo::Foo()’
一种解决方案是使用 std::decval<T>()
Converts any type T to a reference type, making it possible to use member functions in decltype expressions without the need to go through constructors.
因此替换:
template <typename T>
struct Detect_My_Method<T, decltype(T().my_method())>
: std::true_type
{
};
来自
template <typename T>
struct Detect_My_Method<T, decltype(std::declval<T>().my_method())>
: std::true_type
{
};
解决问题。
经验教训:
decltype(Foo().my_method()); // invalid
decltype(std::declval<Foo>().my_method()); // fine
不等价。
此外,还有另一种定义测试的方法,既不需要指向对象的引用或指针,也不需要函数的特定签名:
template<class T>
typename std::is_member_function_pointer<decltype(&T::my_method)>::type test_member_function_my_method(int);
template<class T>
std::false_type test_member_function_my_method(...);
template<class T>
using has_member_function_my_method = decltype(test_member_function_my_method<T>(0));
用法:
static_assert(!has_member_function_my_method<double>::value, "");
static_assert(has_member_function_my_method<Foo>::value, "");