C ++标准中14.8.2第3和第4段的含义是什么? [英] What is the meaning of 14.8.2 paragraphs 3 and 4 in the C++ Standard?

查看:122
本文介绍了C ++标准中14.8.2第3和第4段的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我很难理解这个规则,特别是下面粗体的句子(我强调):



2在下面的代码片段:什么意思是说函数类型是 f(int),但 t const


2/3


执行此替换后, 8.3.5。
[示例: void()(const int,int [5])的参数类型变为 void (int,int *)
-end example] [注意:函数参数声明中的顶级限定符不影响函数
类型,但仍会影响函数中函数参数变量的类型。 -end note
] [示例:

  template< class T& void f(T t); 
template< class X> void g(const X x);
template< class Z> void h(Z,Z *);
int main(){
//#1:function type is f(int),t is non const
f< int>(1)
//#2:function type is f(int),t is const
f< const int>(1);
//#3:function type is g(int),x is const
g< int>(1);
//#4:function type is g(int),x is const
g< const int>(1)
//#5:function type is h(int,const int *)
h< const int>(1,0);

}



/ p>







§14.8.2/ 4


f f (1)调用不同的函数甚至
-end
note]




解决方案

考虑:

  void f(T t){t = 5; } 

f< int&形成,但 f 不是,因为它试图分配一个 const 变量。 >

请参阅:使用'const'函数参数


I'm struggling to understand this rule, specially the sentences in bold below (my emphasis):

Consider the comment #2 in the snippet below: what does it mean to say that the function type is f(int), but t is const?

§14.8.2/3:

After this substitution is performed, the function parameter type adjustments described in 8.3.5 are performed. [ Example: A parameter type of "void ()(const int, int[5])" becomes "void(*)(int,int*)". —end example ] [ Note: A top-level qualifier in a function parameter declaration does not affect the function type but still affects the type of the function parameter variable within the function. —end note ] [ Example:

template <class T> void f(T t);
template <class X> void g(const X x);
template <class Z> void h(Z, Z*);
int main() {
    // #1: function type is f(int), t is non const
    f<int>(1);
    // #2: function type is f(int), t is const
    f<const int>(1);
    // #3: function type is g(int), x is const
    g<int>(1);
    // #4: function type is g(int), x is const
    g<const int>(1);
    // #5: function type is h(int, const int*)
    h<const int>(1,0);

}

—end example ]

§14.8.2/4:

[ Note: f<int>(1) and f<const int>(1) call distinct functions even though both of the functions called have the same function type. —end note ]

解决方案

Consider:

template <class T> void f(T t) { t = 5; }

f<int> is well-formed, but f<const int> is not, because it attempts to assign to a const variable.

See: Use of 'const' for function parameters

这篇关于C ++标准中14.8.2第3和第4段的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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