为什么 `std::remove_cv<_Iter>::type` 不是类型?

Mik*_*ail 2 c++ template-meta-programming c++20

我有两个版本的功能,但 gcc 说版本 1 有效,而版本 2 给出

expected a type, got 'std::remove_cv<_Iter>::type'
Run Code Online (Sandbox Code Playgroud)

我不太明白这个错误,因为我希望该using语句需要一个类型,并且不会自动将 a 提升'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;
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

相关神箭

为了好玩而编辑,它看​​起来像 clang 和 gcc 对 using 关键字的解释不同(参见https://godbolt.org/z/P9Pcn6

son*_*yao 5

您需要使用关键字typename来告诉依赖名称 std::remove_cv<V>::type是一种类型。

return std::is_same<u_underlying, typename std::remove_cv<V>::type>::value;
//                                ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

自 C++20 起不再需要该using语句。typename

在某些上下文中,只有类型名称才能有效出现。在这些上下文中,假定使用依赖限定名称来命名类型,并且不需要 typename

  • Clang 尚未实现,请参阅 cppreference 上的 [P0634R3](https://en.cppreference.com/w/cpp/compiler_support)。 (2认同)