`const T` 和 `T` 在取其嵌套类型时没有区别吗?
Do `const T` and `T` have no difference when taking its nested type?
#include <string>
template<typename T, typename C, typename CR>
void f()
{
typename T::size_type* p1{}; // ok
typename CR::size_type* p2{}; // error
typename C::size_type* p3{}; // Does the C++ standard allow this?
}
int main()
{
f<std::string, const std::string, const std::string&>();
}
Do const T
和 T
取其嵌套类型时没有区别?
确实"nested types"是一样的
用 const
and/or volatile
限定的类型是 "version" 不合格类型([basic.type.qualifier] in the standard, 6.3.8, paragraph 1) - even if it's not quite the same. This is unlike a pointer or a reference, which, when introduced, form a wholly different type than the type they point or refer to (clauses [dcl.ref] and [dcl.ptr] 标准,9.3.3.1 和 9.3 .3.2,两者的第 1 段)。
还值得一提的是,class-scope 类型不会获得 const
-qualified,因为您是从类型的 const
版本中获得它们的 - 例如std::vector<int>::iterator
与 std::add_const_t<std::vector<int>>::iterator
是完全相同的类型 - 但 不是 与 std::vector<int>::const_iterator
.
相同的类型
#include <string>
template<typename T, typename C, typename CR>
void f()
{
typename T::size_type* p1{}; // ok
typename CR::size_type* p2{}; // error
typename C::size_type* p3{}; // Does the C++ standard allow this?
}
int main()
{
f<std::string, const std::string, const std::string&>();
}
Do const T
和 T
取其嵌套类型时没有区别?
确实"nested types"是一样的
用 const
and/or volatile
限定的类型是 "version" 不合格类型([basic.type.qualifier] in the standard, 6.3.8, paragraph 1) - even if it's not quite the same. This is unlike a pointer or a reference, which, when introduced, form a wholly different type than the type they point or refer to (clauses [dcl.ref] and [dcl.ptr] 标准,9.3.3.1 和 9.3 .3.2,两者的第 1 段)。
还值得一提的是,class-scope 类型不会获得 const
-qualified,因为您是从类型的 const
版本中获得它们的 - 例如std::vector<int>::iterator
与 std::add_const_t<std::vector<int>>::iterator
是完全相同的类型 - 但 不是 与 std::vector<int>::const_iterator
.