检查类型是否在 C++ 中定义了 [][]
Checking if a type has [][] defined in C++
我正在编写一个构造函数,并希望为任何实现整型参数[][]
的类型定义它。换句话说,T
必须定义T[]
类型,并且在T
本身上使用[]
时返回的类型也[]
定义,例如,允许调用T[2][3]
。
我知道如何使用此处的答案检查重载运算符是否存在,该答案显示了operator==
的方法,这可以很容易地扩展到我的情况。
但是,问题是我想检查T[]
的类型是否也具有operator[]
。
如前所述,我还需要检查重载operator[]
接受的参数是否包含任何整数类型,因此operator[](const T& i)
会有一些T
,从而产生std::is_integral<T>::value
为真。
显然,我的目的是允许接口接受任何行为类似于矩阵的东西,并具有访问其元素的标准方法。
你可以 sfinae 例如在std::declval<const T>()[1][1]
template<class T, class = decltype(std::declval<const T&>()[1][1])>
void foo(const T& matrix){
...
}
或:
template<class T, decltype(void(std::declval<const T&>()[1][1]),true)=true>
void foo(const T& matrix){
...
}
如果您选择了多个 SFINAE foo
重载,则效果更好。
*(我在手机上,所以我还没有用编译器检查过。
这里有两种方法可以做到这一点。第一个不使用任何外部库,并且使用与您发布的链接答案相同的想法。第二个使用boost.hana库中的功能。
#include <iostream>
#include <type_traits>
#include <vector>
#include <boost/hana/type.hpp>
// SFINAE trick, not using any external libraries
template <class X>
class check {
// this overload will be selected whenever X[][] is defined
template <class T>
static auto call(T*) -> decltype(std::declval<T>()[1][1], std::true_type{});
// and this when X[][] is not defined
template <class T>
static std::false_type call(...);
public:
using type = decltype (call<X>(0));
};
// using boost.hana library
template <class X>
class hana_check {
static auto call() {
auto test = boost::hana::is_valid([](auto&& x) -> decltype (x[1][1]) {});
return decltype(test(std::declval<X>())){};
}
public:
using type = decltype (call());
};
using test_type1 = check<int>::type; // false_type
using test_type2 = check<std::vector<std::vector<int>>>::type; // true_type
using test_type3 = hana_check<double>::type; // false_type
using test_type4 = hana_check<std::vector<std::vector<double>>>::type; // true_type
int main() {
std::cout << test_type1::value << std::endl;
std::cout << test_type2::value << std::endl;
std::cout << test_type3::value << std::endl;
std::cout << test_type4::value << std::endl;
}
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何定义一个纯抽象基类