【发布时间】:2021-06-16 10:43:01
【问题描述】:
我有两个版本的我希望是相同的功能,但 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)
【问题讨论】:
-
TL;欺骗的博士,没有
typename,语言假定std::remove_cv<U>::type是一个值。 -
@NathanOliver 嗯,这个骗局不是很明显。 typename 会去哪里(它已经在签名中)以及为什么
using可以工作,但当它是模板的一部分时却不行... -
typename总是作为名称的前缀,所以using u_underlying = typename std::remove_cv<U>::type; -
@NathanOliver 或者应该是
return std::is_same<u_underlying,typename std::remove_cv<V>::type>::value; -
我重新打开了这个问题,因为它似乎关注
typename在using语句和return语句中的用法之间的区别。
标签: c++ template-meta-programming c++20