重载决策中的 const 指针
GCC 将这两个函数声明视为等效:
void F(int* a) { }
void F(int* const a) { }
test.cpp:在函数“void F(int*)”中:
test.cpp:235: 错误:重新定义“void F(int*)”
test.cpp:234: 错误:之前在此处定义的“void F(int*)”
这是有道理的,因为在这种情况下调用者总是会忽略 const...它只影响函数内部参数“a”的使用。
我想知道的是,标准在哪里(如果有的话)说,为了重载解析的目的,可以丢弃用作函数参数的指针上的限定符。
(我真正的问题是,我想弄清楚 GCC 在内部哪里删除了这些毫无意义的限定符,并且由于 GCC 的 C++ 前端充斥着引用该标准的注释,该标准的相关部分可能会帮助我找到正确的位置。 )
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
标准在 8.3.5/3 中表示,为了确定函数类型,任何直接限定参数类型的 cv 限定符都将被删除。即,它字面意思是声明为的函数
具有函数类型
void (int *)
。迂腐的人可能会争辩说,这还不够结论性,不足以声称上述声明应该与这样的定义相匹配,
或者它应该使具有双重声明的代码(如您的示例中)格式错误,因为这些概念都不是标准中用函数类型进行了描述。
我的意思是,我们都知道这些 const 是为了所有外部目的而被忽略的,但到目前为止我无法在标准中找到能够明确说明这一点的措辞。也许我错过了什么。
实际上,在 13.1/3 中,它有一个“注释”,表示具有等效参数声明(如 8.3.5 中定义)的函数声明声明相同的函数。但这只是一个注释,它是非规范性的,这表明标准中的某个地方应该有一些关于同一问题的规范性文本。
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
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
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.
我认为它基本上是被禁止的:
非引用上的 const 不参与重载。
事实上,您甚至可以声明
并稍后定义
常量纯粹是调用者不关心的实现细节。
I think it is basically as prohibited as this:
const on non-references doesn't participate in overloading.
In fact you could even declare
and later define
where the constness is purely an implementation detail which the caller doesn't care about.
它等同于:
对调用者来说是相同的。这是因为无论如何,该函数都会按值获取副本,因此调用者并不关心它是否被认为是 const ;这没有什么区别。
编译器忽略这些事情是不合法的,但是重载解析没有区别。
const
适用于函数的实现。如果编译器处理如下,则非法:
相同,忽略
const
。It's the same as:
Being the same to the caller. This is because the function is getting a copy by-value no matter what, so the caller doesn't care if it's thought of as
const
or not; it makes no difference to it.It's not legal for the compiler to ignore such things, but there is no difference in overload resolution. The
const
applies to the implementation of the function.Illegal would be if the compiler treated:
The same, by ignoring the
const
.我相信情况恰恰相反。任何指针,即使是非 const,都可以像 const 一样对待:)。
I believe it's the other way around. Any pointer, even nonconst, can be treated like const :).