#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&>();
}
`const T`和`T`在取其嵌套类型时是否没有区别? const T
和 T
取其嵌套类型时没有区别?
一个用 const
或 volatile
是非限定类型的 "版本"([basic.type.qualifier] 在标准中,6.3.8,第 1 段)--即使它不太一样。这与指针或引用不同,当引入指针或引用时,它们所形成的类型与它们所指向或引用的类型完全不同(从句 [dcl.ref] 和 [dcl.ptr] 标准,9.3.3.1和9.3.3.2,两个标准的第1段)。)
还值得一提的是,类作用域类型不会得到 const
-合格的,因为你从 const
类型的版本--例如 std::vector<int>::iterator
是完全相同的类型 std::add_const_t<std::vector<int>>::iterator
- 不过 不 同类 std::vector<int>::const_iterator
.