常量指针在重载分辨率 [英] const pointers in overload resolution

查看:148
本文介绍了常量指针在重载分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC将这两个函数声明视为等效的:

  void F(int * a){} 
void F(int * const a){}




test.cpp:In function'void F(int *)':



test.cpp:235:error:redefinition' b

test.cpp:234:error:'void F(int *)'以前在此处定义


这有一些意义,因为调用者在这种情况下总是忽略const ...它只影响函数内部的参数'a'的用法。



我想知道是在哪里(如果在任何地方)的标准说,它是特别确定放弃指针的限定符作为函数参数用于重载解析的目的。



真正的问题是,我想弄清楚GCC在内部去掉这些无意义的限定符,并且由于GCC的C ++前端散布着引用该标准的注释,标准的相关部分可能会帮助我找到正确的位置。)

解决方案

Standard在8.3.5 / 3中说,用于确定函数类型 any cv直接限定参数类型的限定符将被删除。也就是说它字面上说一个函数声明为

  void foo(int * const a); 

有函数类型 void(int *)



一个拙劣的人可能会认为这不足以声称上述声明应该像这样的定义匹配。

  void foo(int * a)
{
}

或者它应该使代码带有双重声明(如在你的例子中)是不成形的,因为这些概念都不是在标准中描述的函数类型



我的意思是,我们都知道这些 const 到目前为止,我无法找到在标准中的语句,将决定性地确切地说明。也许我错过了一些东西。



实际上,在13.1 / 3中,它有一个注释,说具有等效参数声明的函数声明相同的功能。但它只是一个注释,它是非规范性的,这表明在标准的某个地方应该有同样的问题的一些规范性文本。


GCC treats these two function declarations as equivalent:

void F(int* a) { }
void F(int* const a) { }

test.cpp: In function 'void F(int*)':

test.cpp:235: error: redefinition of 'void F(int*)'

test.cpp:234: error: 'void F(int*)' previously defined here

This makes some sense because a caller will always ignore the const in this case... it only affects the usage of the parameter 'a' inside of the function.

What I'm wondering is where (if anywhere) the standard says that it's specifically OK to discard qualifiers on pointers used as function arguments for the purpose of overload resolution.

(My real issue is that I'd like to figure out where GCC strips these pointless qualifiers internally, and since the C++ frontend of GCC is littered with comments referencing the standard, the relevant section of the standard might help me find the correct spot.)

解决方案

Standard says in 8.3.5/3 that for the purposes of determining the function type any cv-qualifiers that directly qualify the parameter type are deleted. I.e. it literally says that a function declared as

void foo(int *const a);

has function type void (int *).

A pedantic person might argue that this is not conclusive enough to claim that the above declaration should match the definition like this one

void foo(int *a)
{
}

or that it should make the code with dual declaration (as in your example) ill-formed, since neither of these concepts are described in the standard in terms of function types.

I mean, we all know that these const were intended to be ignored for all external purposes, but so far I was unable to find the wording in the standard that would conclusively state exactly that. Maybe I missed something.

Actually, in 13.1/3 it has a "Note" that says that function declarations with equivalent parameter declarations (as defined in 8.3.5) declare the same function. But it is just a note, it is non-normative, which suggests that somewhere in the standard there should be some normative text on the same issue.

这篇关于常量指针在重载分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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