为什么 `std::remove_cv<_Iter>::type` 不是类型?
Why is a `std::remove_cv<_Iter>::type` not a type?
我有两个版本的功能相同,但 gcc 说版本 1 有效,而版本 2 给出
expected a type, got 'std::remove_cv<_Iter>::type'
我不太明白这个错误,因为我希望 using
语句需要一个类型,并且不会自动将 'std::remove_cv<_Iter>::type'
提升为其他类型?
有人可以解释一下这是怎么回事吗?
template<typename U,typename V> constexpr inline auto is_same_rcv() noexcept
{
//version 1 works
using u_underlying = std::remove_cv<U>::type;
using v_underlying = std::remove_cv<V>::type;
return std::is_same<u_underlying,v_underlying>::value;
}
和
template<typename U,typename V> constexpr inline auto is_same_rcv() noexcept
{
//version 2 doesn't work
using u_underlying = std::remove_cv<U>::type;
return std::is_same<u_underlying,std::remove_cv<V>::type>::value;
}
关联godbolt
编辑一下,好像 clang 和 gcc 在 using 关键字的解释上有所不同(参见 https://godbolt.org/z/P9Pcn6)
需要使用关键字typename
to tell that the dependent names std::remove_cv<V>::type
是一种类型
return std::is_same<u_underlying, typename std::remove_cv<V>::type>::value;
// ^^^^^^^^
在 using
语句中,自 C++20 起不再需要 typename
。
In some contexts, only type names can validly appear. In these
contexts, a dependent qualified name is assumed to name a type and no
typename
is required:
...
A qualified name that appears in type-id, where the smallest enclosing type-id is:
- ...
- the type-id in an alias declaration;
- ...
我有两个版本的功能相同,但 gcc 说版本 1 有效,而版本 2 给出
expected a type, got 'std::remove_cv<_Iter>::type'
我不太明白这个错误,因为我希望 using
语句需要一个类型,并且不会自动将 'std::remove_cv<_Iter>::type'
提升为其他类型?
有人可以解释一下这是怎么回事吗?
template<typename U,typename V> constexpr inline auto is_same_rcv() noexcept
{
//version 1 works
using u_underlying = std::remove_cv<U>::type;
using v_underlying = std::remove_cv<V>::type;
return std::is_same<u_underlying,v_underlying>::value;
}
和
template<typename U,typename V> constexpr inline auto is_same_rcv() noexcept
{
//version 2 doesn't work
using u_underlying = std::remove_cv<U>::type;
return std::is_same<u_underlying,std::remove_cv<V>::type>::value;
}
关联godbolt
编辑一下,好像 clang 和 gcc 在 using 关键字的解释上有所不同(参见 https://godbolt.org/z/P9Pcn6)
需要使用关键字typename
to tell that the dependent names std::remove_cv<V>::type
是一种类型
return std::is_same<u_underlying, typename std::remove_cv<V>::type>::value;
// ^^^^^^^^
在 using
语句中,自 C++20 起不再需要 typename
。
In some contexts, only type names can validly appear. In these contexts, a dependent qualified name is assumed to name a type and no
typename
is required:
...
A qualified name that appears in type-id, where the smallest enclosing type-id is:
- ...
- the type-id in an alias declaration;
- ...