std::declval<T>(), SFINAE and deleted constructor

std::declval<T>(), SFINAE and deleted constructor

本文关键字:SFINAE and deleted constructor gt declval lt std      更新时间:2023-10-16

在 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>()

将任何类型 T 转换为引用类型,从而可以使用 decltype 表达式中的成员函数,无需 通过构造函数

因此替换:

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, "");