鉴于(减少)实施检测惯用语 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
namespace type_traits
{
template<typename... Ts>
using void_t = void;
namespace detail
{
template<typename, template<typename...> class, typename...>
struct is_detected : std::false_type {};
template<template<class...> class Operation, typename... Arguments>
struct is_detected<void_t<Operation<Arguments...>>, Operation, Arguments...> : std::true_type {};
}
template<template<class...> class Operation, typename... Arguments>
using is_detected = detail::is_detected<void_t<>, Operation, Arguments...>;
template<template<class...> class Operation, typename... Arguments>
constexpr bool is_detected_v = detail::is_detected<void_t<>, Operation, Arguments...>::value;
}
我们可以很容易地检查一个类是否foo
包含一个成员函数bar
struct foo {
int const& bar(int&&) { return 0; }
};
template<class T>
using bar_t = decltype(std::declval<T>().bar(0));
int main()
{
static_assert(type_traits::is_detected_v<bar_t, foo>, "not detected");
return 0;
}
然而,正如你所看到的,我们无法检测到foo::bar
的参数类型是int&&
。检测成功,原因0
可以传递给foo::bar
。我知道有很多选项可以检查exact(成员)函数的签名。但我想知道是否可以修改此检测工具包以检测到foo::bar
的参数类型正是int&&
.
[我创建了一个现场演示 http://coliru.stacked-crooked.com/a/207d4486edb89ac3这个例子。]