如何检查下标操作符是否存在
How to check for the existence of a subscript operator?
我想写一个类型trait,它使用SFINAE检查类型是否存在下标表达式。当下标表达式可能时,我下面的初始尝试似乎可以工作,但当括号操作符不存在时,则无法工作。
#include <iostream>
#include <vector>
#include <cassert>
template<class T, class Index>
struct has_subscript_operator_impl
{
template<class T1,
class Reference = decltype(
(*std::declval<T*>())[std::declval<Index>()]
),
class = typename std::enable_if<
!std::is_void<Reference>::value
>::type>
static std::true_type test(int);
template<class>
static std::false_type test(...);
using type = decltype(test<T>(0));
};
template<class T, class Index>
using has_subscript_operator = typename has_subscript_operator_impl<T,Index>::type;
struct doesnt_have_it {};
struct returns_void
{
void operator[](int) {}
};
struct returns_int
{
int operator[](int) { return 0; }
};
int main()
{
std::cout << "has_subscript_operator<doesnt_have_it,int>: " << has_subscript_operator<doesnt_have_it,int>::value << std::endl;
assert((!has_subscript_operator<doesnt_have_it,int>::value));
std::cout << "has_subscript_operator<returns_void,int>: " << has_subscript_operator<returns_void,int>::value << std::endl;
assert((!has_subscript_operator<returns_void,int>::value));
std::cout << "has_subscript_operator<returns_int,int>: " << has_subscript_operator<returns_int,int>::value << std::endl;
assert((has_subscript_operator<returns_int,int>::value));
std::cout << "has_subscript_operator<int*,int>: " << has_subscript_operator<int*,int>::value << std::endl;
assert((has_subscript_operator<int*,int>::value));
std::cout << "has_subscript_operator<std::vector<int>,int>: " << has_subscript_operator<std::vector<int>,int>::value << std::endl;
assert((has_subscript_operator<returns_int,int>::value));
return 0;
}
clang-3.4
输出:
$ clang -std=c++11 -I. -lstdc++ test_has_subscript_operator.cpp
test_has_subscript_operator.cpp:10:14: error: type 'doesnt_have_it' does not provide a subscript operator
(*std::declval<T*>())[std::declval<Index>()]
^~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
test_has_subscript_operator.cpp:25:1: note: in instantiation of template class 'has_subscript_operator_impl<doesnt_have_it, int>' requested here
using has_subscript_operator = typename has_subscript_operator_impl<T,Index>::type;
^
test_has_subscript_operator.cpp:41:66: note: in instantiation of template type alias 'has_subscript_operator' requested here
std::cout << "has_subscript_operator<doesnt_have_it,int>: " << has_subscript_operator<doesnt_have_it,int>::value << std::endl;
^
1 error generated.
我如何修复我的has_subscript_operator
,使其正确地为所有类型工作?
SFINAE仅在直接上下文中发生替换失败时起作用。在实例化成员函数模板test
时,模板参数Index
已经是已知的,因此您得到的不是替换失败,而是硬错误。
解决这个问题的技巧是通过向test
添加一个额外的模板类型参数并将其默认为Index
来再次推断Index
。
template<class T1,
class IndexDeduced = Index, // <--- here
class Reference = decltype(
(*std::declval<T*>())[std::declval<IndexDeduced>()] // and use that here
),
class = typename std::enable_if<
!std::is_void<Reference>::value
>::type>
static std::true_type test(int);
现在你的代码可以正常工作了。
现场演示
一旦你有了c++ 11,写类型特征就容易多了…您不需要使用省略号重载技巧。您可以直接使用您的decltype
表达式在神奇的帮助下:
template <typename... >
using void_t = void;
我们有我们的基本情况:
template<class T, class Index, typename = void>
struct has_subscript_operator : std::false_type { };
和我们的表达式SFINAE的有效情况:
template<class T, class Index>
struct has_subscript_operator<T, Index, void_t<
decltype(std::declval<T>()[std::declval<Index>()])
>> : std::true_type { };
然后你可以写同样的别名:
template <class T, class Index>
using has_subscript_operator_t = typename has_subscript_operator<T, Index>::type;
您还可以使用@Yakk最喜欢的方法,该方法给出了他在每个答案中复制的样板文件:
namespace details {
template<class...>struct voider{using type=void;};
template<class...Ts>using void_t=typename voider<Ts...>::type;
template<template<class...>class Z, class, class...Ts>
struct can_apply:
std::false_type
{};
template<template<class...>class Z, class...Ts>
struct can_apply<Z, void_t<Z<Ts...>>, Ts...>:
std::true_type
{};
}
template<template<class...>class Z, class...Ts>
using can_apply=details::can_apply<Z,void,Ts...>;
你可以简单地写属性:
template <class T, class Index>
using subscript_t = decltype(std::declval<T>()[std::declval<Index>()]);
template <class T, class Index>
using has_subscript = can_apply<subscript_t, T, Index>;
与c++ 20的概念:
template<class T, class I = std::size_t>
concept indexible = requires (T& t, const I& i) {
{t[i]};
};
用法:
static_assert(not indexible<std::list<int>>);
static_assert( indexible<std::string>);
static_assert( indexible<std::vector<bool>>);
static_assert(not indexible<std::map<std::string, int>>);
static_assert( indexible<std::map<std::string, int>, std::string>);
static_assert(not indexible<const std::map<std::string, int>, std::string>);
struct any {
template<class T>
operator const T&() const;
};
static_assert( indexible<std::map<std::string, int>, any>);
static_assert( indexible<std::map<char*, int>, any>);
struct Foo {
int operator[](int i);
int operator[](double i);
};
static_assert(not indexible<Foo, any>);
实时演示:https://godbolt.org/z/7GWK4PvM8
请注意,如果operator[]
重载了不同的参数类型,则any
的使用将失败。
相关文章:
- 下标操作符重载以访问私有数组
- 通过重载下标操作符()从矩阵中获取向量
- 重载c++下标[]操作符,可以使用长int索引
- 下标操作符重载:返回引用问题
- 矩阵上的数组下标操作符
- 用于部分模板特化的下标操作符重载
- 重载下标操作符
- c++ std::map::iterator一无所获,但下标操作符返回对映射值的引用
- 在模板双链表类中重载下标操作符
- 在使用c++类的下标操作符传递的数组中使用关系操作符
- 通过不返回引用来防止下标操作符的赋值会导致运行时崩溃
- 重载c++中的下标[]操作符来设置类(量词)的大小
- 如何重载下标操作符以允许RPG字符数组
- 重载数组下标操作符
- 在2D数组中通过重载数组下标操作符进行访问
- 如何重载具有读写功能的下标操作符
- 为什么下标操作符在c++中经常成对出现
- c++两个版本的重载下标操作符
- Boost c++ Phoenix用户定义参数的下标操作符[]错误
- 如何检查下标操作符是否存在