std::is_same and std::get together
std::is_same and std::get together
我试图检查特定类型是否进入类型列表(元组(。并遇到了一些麻烦:
#include <tuple>
#include <iostream>
template < typename T >
struct Type {
using type = T;
};
int main() {
constexpr auto Foo = std::make_tuple(Type<int>{},Type<int>{});
if (std::is_same<int, std::get<0>(Foo)::type>::value)
std::cout << "GG!" << std::endl;
return 0;
}
test.cpp: In function ‘int main()’:
test.cpp:13:47: error: type/value mismatch at argument 2 in template parameter list for ‘template<class, class> struct std::is_same’
if (std::is_same<int, std::get<0>(Foo)::type>::value)
^
test.cpp:13:47: note: expected a type, got ‘std::get<0ul, {Type<int>, Type<int>}>(Foo)’
似乎std::get
没有像我想要的那样向我展示类型。有人可以解释我为什么吗?
std::get<0>(Foo)::type
在此上下文中没有任何意义,因为std::get<0>(Foo)
是一个值,而不是一个类型。
相反,请尝试以下操作:我们将使用 decltype()
来获取该表达式的类型(无需实际评估其值(。 这将产生Type<int>&
类型。 我们将使用 std::decay
删除引用(因为Type<int>&::type
也没有意义(,然后我们可以访问其type
typedef。
这有点笨拙,但它有效:
if (
std::is_same<
int,
// The decltype will be (Type<int>&). Decay to remove the
// reference, cv-qualifiers, etc.
std::decay<
decltype(std::get<0>(Foo))
>::type::type
// ^ Two ::types, one to extract the type from std::decay, one
// for your Type<T> struct.
>::value
) {
std::cout << "GG!" << std::endl;
}
(演示(
当然可能有更好的方法来完成此检查,但这是一种方法。
相关文章:
- std::get like (partial) template specialization
- 为什么 ADL 不能使用 std::get 解析为正确的函数
- 为什么不能使用std :: get()获得向量的成员
- std::get from teplate argument
- static_cast 使用 std::get 与枚举类时必需的
- 为什么 std::get 不适用于变量?
- std :: is_same在比较std :: tuple_element和declType(std :: get)时返
- 完美地从 std::get 中获取元组值
- 元组 std::get() 不适用于变量定义的常量
- 为什么 std::get 没有一个接受转发引用的签名
- std::is_same and std::get together
- 使用派生自 'std::tuple' 的元素在 'std::tuple' 上调用 'std::get' - 格式不正确
- 如何使用 std::get 作为 boost-multi-index 容器键的global_fun
- 让"std::get"与SFINAE玩得很好
- 从std::tuple派生时出现混乱,无法处理std::get
- 关于 std::tuple 对象上 std:get() 的返回类型的混淆
- 使用 std::get 作为 std::transform 的参数
- std::get 使用枚举类作为模板参数
- std::get 在给定 6 个整数元组时失败
- volatile and const volatile std::tuple and std::get