首页 > 解决方案 > 为什么 `std::remove_cv<_Iter>::type` 不是类型?

问题描述

我有两个版本的我希望是相同的功能,但 gcc 说版本 1 是有效的,而版本 2 给出了一个

expected a type, got 'std::remove_cv<_Iter>::type'

我不太明白这个错误,因为我希望该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;
}

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;
}

相关的神螺栓

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

标签: c++template-meta-programmingc++20

解决方案


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

return std::is_same<u_underlying, typename std::remove_cv<V>::type>::value;
//                                ^^^^^^^^

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

在某些情况下,只有类型名称才能有效出现。在这些上下文中,假定依赖限定名称来命名类型,并且 no typename是必需的:

  • ...

  • 出现在type-id中的限定名称,其中最小的封闭type-id是:


推荐阅读