如何在检测习惯用法中要求确切的功能签名? [英] How to require an exact function signature in the detection idiom?
问题描述
让我们假设我有一个 T
类型,并且我想检测它是否有一个下标运算符,可以用其他类型的 Index
。以下示例可以正常工作:
Let's suppose I have a type T
and I want to detect whether it has a subscript operator which I can call with with another type Index
. The following example works just fine:
#include <type_traits>
#include <vector>
template < typename T, typename Index >
using subscript_t = decltype(std::declval<T>()[std::declval<Index>()]);
int main()
{
using a = subscript_t< std::vector<int>, size_t >;
using b = subscript_t< std::vector<int>, int >;
}
但是,我希望检测该功能当且仅当函数签名完全匹配时。在上面的示例中,我希望语句 subscript_t< std :: vector< int> ;, int> ;;
抛出类似 no可行的重载运算符[]
之类的错误,因为下标的签名 std :: vector
的运算符是
However, I want the function to be detected if and only if the function signature matches exactly. In the example above I would like the statement subscript_t< std::vector<int>, int >;
to throw an error like no viable overloaded operator[]
, because the signature of the subscript operator for std::vector
is
std::vector<T, std::allocator<T>>::operator[](size_type pos);
其中GCC中的 size_type
是无符号长
。如何避免发生从 int
到 size_t
的隐式转换?
where size_type
in GCC is unsigned long
. How can I avoid the implicit conversion from int
to size_t
to take place?
推荐答案
使用 被检测到
,您可以这样做:
With is_detected
, you may do:
template <typename T, typename Ret, typename Index>
using subscript_t = std::integral_constant<Ret (T::*) (Index), & T::operator[]>;
template <typename T, typename Ret, typename Index>
using has_subscript = is_detected<subscript_t, T, Ret, Index>;
static_assert(has_subscript<std::vector<int>, int&, std::size_t>::value, "!");
static_assert(!has_subscript<std::vector<int>, int&, int>::value, "!");
我在SO文档中这样写:
I wrote this in SO Documentation:
概括类型特征创建:基于SFINAE
有实验特征 detected_or
, detected_t
, is_detected
。
To generalize type_trait creation:based on SFINAE
there are experimental traits detected_or
, detected_t
, is_detected
.
使用模板参数 typename默认值
,模板< typename ...> Op
和 typename ... Args
:
-
被检测到
:std :: true_type
或std :: false_type $ c $的别名c>取决于
Op< Args ...>
-
detected_t $ c的有效性$ c>:
Op< Args ...>
或nonesuch
的别名,具体取决于Op< Args ...>
。 -
detected_or
:具有value_t
被检测到类型
$ c> Op< Args ...> 或默认
,具体取决于Op< Args ...> ;
is_detected
: alias ofstd::true_type
orstd::false_type
depending of the validity ofOp<Args...>
detected_t
: alias ofOp<Args...>
ornonesuch
depending of validity ofOp<Args...>
.detected_or
: alias of a struct withvalue_t
which isis_detected
, andtype
which isOp<Args...>
orDefault
depending of validity ofOp<Args...>
可以使用 std :: void_t $ c实现SFINAE的$ c>如下:
namespace detail {
template <class Default, class AlwaysVoid,
template<class...> class Op, class... Args>
struct detector
{
using value_t = std::false_type;
using type = Default;
};
template <class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
{
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
// special type to indicate detection failure
struct nonesuch {
nonesuch() = delete;
~nonesuch() = delete;
nonesuch(nonesuch const&) = delete;
void operator=(nonesuch const&) = delete;
};
template <template<class...> class Op, class... Args>
using is_detected =
typename detail::detector<nonesuch, void, Op, Args...>::value_t;
template <template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;
template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;
检测方法是否存在的特征可以简单地实现: / p>
Traits to detect presence of method can then be simply implemented:
template <typename T, typename ...Ts>
using foo_type = decltype(std::declval<T>().foo(std::declval<Ts>()...));
struct C1 {};
struct C2 {
int foo(char) const;
};
template <typename T>
using has_foo_char = is_detected<foo_type, T, char>;
static_assert(!has_foo_char<C1>::value, "Unexpected");
static_assert(has_foo_char<C2>::value, "Unexpected");
static_assert(std::is_same<int, detected_t<foo_type, C2, char>>::value,
"Unexpected");
static_assert(std::is_same<void, // Default
detected_or<void, foo_type, C1, char>>::value,
"Unexpected");
static_assert(std::is_same<int, detected_or<void, foo_type, C2, char>>::value,
"Unexpected");
这篇关于如何在检测习惯用法中要求确切的功能签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!