const T和T在使用嵌套类型时是否没有区别? [英] Do `const T` and `T` have no difference when taking its nested type?

查看:44
本文介绍了const T和T在使用嵌套类型时是否没有区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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 和/或 volatile 限定的类型是非限定类型的版本"( [dcl.ptr] (该标准的9.3.3.1和9.3.3.2,均属于第1段).

Indeed, the "nested types" are the same.

A type qualified with const and/or volatile is a "version" of the unqualified type ([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] of the standard, 9.3.3.1 and 9.3.3.2, paragraph 1 in both).

还值得一提的是,类作用域类型没有获得 const 限定,因为您是从该类型的 const 版本中获得的-例如 std :: vector< int> :: iterator std :: add_const_t< std :: vector< int>:iterator 的类型完全相同-但不是 std :: vector< int> ::: const_iterator 的类型相同.

It is also worth mentioning that class-scope types do not get const-qualified because you get them from the const version of the type - e.g. std::vector<int>::iterator is the exact same type as std::add_const_t<std::vector<int>>::iterator - but not the same type as std::vector<int>::const_iterator.

这篇关于const T和T在使用嵌套类型时是否没有区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆