`const T`和`T`在取其嵌套类型时没有区别吗?

问题描述 投票:1回答:1
#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 取其嵌套类型时没有区别?

c++ templates language-lawyer inner-classes
1个回答
5
投票

事实上,"嵌套类型 "是一样的。

一个用 constvolatile 是非限定类型的 "版本"([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.

© www.soinside.com 2019 - 2024. All rights reserved.