C ++标准中14.8.2第3和第4段的含义是什么? [英] What is the meaning of 14.8.2 paragraphs 3 and 4 in the C++ Standard?
问题描述
我很难理解这个规则,特别是下面粗体的句子(我强调):
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; }
请参阅:使用'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 After this substitution is performed, the function parameter type adjustments described in 8.3.5 are performed.
[ Example: A parameter type of " } —end example ]
[ Note:
Consider: See: Use of 'const' for function parameters 这篇关于C ++标准中14.8.2第3和第4段的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! f< int&形成,但
f
const
变量。 >
f(int)
, but t
is const
?
§14.8.2/3
:
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);
§14.8.2/4
:
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 ]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.