std::is_same 无法通过 constexpr 自动变量的 decltype 工作
std::is_same doesn't work through decltype of constexpr auto variable
我试图 static_assert
某些元转换器算法有效,但令人难以置信的是,它没有与相同的算法进行比较,即使 typeid().name()
返回了完全相同的字符串。
在 typedef 中重复类型表达式可以修复 is_same
,但是 我不明白在 typedef 中重复初始化表达式如何改变类型,而不是采用 decltype使用相同表达式初始化的自动变量.
更具体的解释我在做什么:
我做了一个元转换器,可以将元值列表(包含枚举器)转换为所有枚举器的 std::tuple。然后我检查生成的元组是我预期的那个。
下面的所有代码,检查带有注释的行 // works // doesn't work
而最终的考验也在最后。
#include <type_traits>
#include <tuple>
template<typename T> struct ValueListAsTuple; // master template
// destructurer of list:
template<template <auto...> class L, auto... Vs>
struct ValueListAsTuple<L<Vs...>>
{
static constexpr auto value = std::make_tuple(Vs...);
//using type = decltype(std::make_tuple(Vs...)); // works
using type = decltype(value); // doesn't work
};
// template typedef
template<typename T> using ValueListAsTuple_t = typename ValueListAsTuple<T>::type;
struct Kind
{
enum EnumType { E1, E2 };
template <auto... Values> struct MetaVals{}; // meta value list
using MetaValueList = MetaVals<
E1,
E2
>;
};
int main(int argc, const char* argv[])
{
auto tuple = ValueListAsTuple_t< Kind::MetaValueList > {};
//std::cout << typeid(tuple).name() << '\n';
// this prints: "class std::tuple<enum Kind::EnumType, enum Kind::EnumType>"
// manual re-creation of the type, for testing:
std::tuple< Kind::EnumType, Kind::EnumType > t2;
//std::cout << typeid(t2).name() << '\n';
// this prints the exact same thing.
static_assert( std::is_same_v< std::tuple_element<0, decltype(tuple)>::type, Kind::EnumType > );
static_assert( std::is_same_v< std::tuple_element<1, decltype(tuple)>::type, Kind::EnumType > );
// WHAT ???
static_assert( std::is_same_v<
ValueListAsTuple_t< Kind::MetaValueList >,
std::tuple< Kind::EnumType, Kind::EnumType >
> );
// is_convertible_v works but I don't care ! wth ?
}
正如您所见,当通过 value
声明 type
时,is_same
无法推断出相同的类型,但如果我重复 std::make_tuple(..
,它就可以工作。我看不出这两种形式有什么区别。
我什至逐个元素地检查元组的每个索引的类型是否相同。
检查 godbolt 中的动作:
https://godbolt.org/z/QUCXMB
gcc/clang/msvc
中的行为相同
这确实是一个微不足道的错误,但不幸的是很难发现。您遇到的问题是因为 constexpr
意味着 const
。因此,在您的示例中 type
是 const tuple<...>
,它与您检查的非 cv 限定类型不同。别名的简短修复应该可以使您的测试通过:
using type = std::remove_const_t<decltype(value)>;
我试图 static_assert
某些元转换器算法有效,但令人难以置信的是,它没有与相同的算法进行比较,即使 typeid().name()
返回了完全相同的字符串。
在 typedef 中重复类型表达式可以修复 is_same
,但是 我不明白在 typedef 中重复初始化表达式如何改变类型,而不是采用 decltype使用相同表达式初始化的自动变量.
更具体的解释我在做什么:
我做了一个元转换器,可以将元值列表(包含枚举器)转换为所有枚举器的 std::tuple。然后我检查生成的元组是我预期的那个。
下面的所有代码,检查带有注释的行 // works // doesn't work
而最终的考验也在最后。
#include <type_traits>
#include <tuple>
template<typename T> struct ValueListAsTuple; // master template
// destructurer of list:
template<template <auto...> class L, auto... Vs>
struct ValueListAsTuple<L<Vs...>>
{
static constexpr auto value = std::make_tuple(Vs...);
//using type = decltype(std::make_tuple(Vs...)); // works
using type = decltype(value); // doesn't work
};
// template typedef
template<typename T> using ValueListAsTuple_t = typename ValueListAsTuple<T>::type;
struct Kind
{
enum EnumType { E1, E2 };
template <auto... Values> struct MetaVals{}; // meta value list
using MetaValueList = MetaVals<
E1,
E2
>;
};
int main(int argc, const char* argv[])
{
auto tuple = ValueListAsTuple_t< Kind::MetaValueList > {};
//std::cout << typeid(tuple).name() << '\n';
// this prints: "class std::tuple<enum Kind::EnumType, enum Kind::EnumType>"
// manual re-creation of the type, for testing:
std::tuple< Kind::EnumType, Kind::EnumType > t2;
//std::cout << typeid(t2).name() << '\n';
// this prints the exact same thing.
static_assert( std::is_same_v< std::tuple_element<0, decltype(tuple)>::type, Kind::EnumType > );
static_assert( std::is_same_v< std::tuple_element<1, decltype(tuple)>::type, Kind::EnumType > );
// WHAT ???
static_assert( std::is_same_v<
ValueListAsTuple_t< Kind::MetaValueList >,
std::tuple< Kind::EnumType, Kind::EnumType >
> );
// is_convertible_v works but I don't care ! wth ?
}
正如您所见,当通过 value
声明 type
时,is_same
无法推断出相同的类型,但如果我重复 std::make_tuple(..
,它就可以工作。我看不出这两种形式有什么区别。
我什至逐个元素地检查元组的每个索引的类型是否相同。
检查 godbolt 中的动作:
https://godbolt.org/z/QUCXMB
gcc/clang/msvc
中的行为相同这确实是一个微不足道的错误,但不幸的是很难发现。您遇到的问题是因为 constexpr
意味着 const
。因此,在您的示例中 type
是 const tuple<...>
,它与您检查的非 cv 限定类型不同。别名的简短修复应该可以使您的测试通过:
using type = std::remove_const_t<decltype(value)>;