C++ 为什么返回类型上的类型限定符毫无意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1607188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is a type qualifier on a return type meaningless?
提问by vehomzzz
Say I have this example:
说我有这个例子:
char const * const
foo( ){
/* which is initialized to const char * const */
return str;
}
What is the right way to do it to avoid the compiler warning "type qualifier on return type is meaningless"?
避免编译器警告“返回类型的类型限定符毫无意义”的正确方法是什么?
回答by Johannes Schaub - litb
The way you wrote it, it was saying "the returned pointer value is const". But non-class type rvalues are not modifiable (inherited from C), and thus the Standard says non-class type rvalues are never const-qualified (right-most const was ignored even tho specified by you) since the const would be kinda redundant. One doesn't write it - example:
你写它的方式是说“返回的指针值是const”。但是非类类型的右值是不可修改的(继承自 C),因此标准说非类类型的右值永远不会被 const 限定(即使你指定了最右边的 const 也被忽略),因为 const 有点多余. 一个不写它 - 例如:
int f();
int main() { f() = 0; } // error anyway!
// const redundant. returned expression still has type "int", even though the
// function-type of g remains "int const()" (potential confusion!)
int const g();
Notice that for the type of "g", the const issignificant, but for rvalue expressions generated from type int const
the const is ignored. So the following is an error:
请注意,对于“g”的类型,const很重要,但对于从类型生成的右值表达式int const
,const 被忽略。所以下面是一个错误:
int const f();
int f() { } // different return type but same parameters
There is no way known to me you could observe the "const" other than getting at the type of "g" itself (and passing &f
to a template and deduce its type, for example). Finally notice that "char const" and "const char" signify the same type. I recommend you to settle with one notion and using that throughout the code.
除了获取“g”本身&f
的类型(例如传递给模板并推断其类型)之外,我没有办法观察到“const” 。最后注意“char const”和“const char”表示相同的类型。我建议您使用一个概念并在整个代码中使用它。
回答by pmg
In C, because function return values, and qualifying values is meaningless.
It may be different in C++, check other answers.
在 C 中,因为函数返回值,而限定值是没有意义的。
在 C++ 中可能会有所不同,请查看其他答案。
const int i = (const int)42; /* meaningless, the 42 is never gonna change */
int const foo(void); /* meaningless, the value returned from foo is never gonna change */
Only objects can be meaningfully qualified.
只有对象才能被有意义地限定。
const int *ip = (const int *)&errno; /* ok, `ip` points to an object qualified with `const` */
const char *foo(void); /* ok, `foo()` returns a pointer to a qualified object */
回答by user1076543
None of the previous answers actually answer the "right way to do it" part of the question.
以前的答案都没有真正回答问题的“正确的方法”部分。
I believe that the answer to this is:
我相信这个问题的答案是:
char const * foo( ){
which says you are returning a pointer a constant character.
这表示您正在返回一个指针常量字符。