C++ 中的 const 混乱

发布于 2024-09-16 02:36:55 字数 655 浏览 2 评论 0原文

可能的重复:
为什么我的返回类型毫无意义?

嗨,我对特定的内容感到困惑常量转换。我有类似

// Returns a pointer that cannot be modified,   
// although the value it points to can be modified.  
double* const foo()  
{  
    static double bar = 3.14;  
    return &bar;  
}

int main()  
{  
    double* const x = foo(); // fine  
    const double* y = foo(); // eh?!  
    return 0;  
}

当我在 MSVS 2008 (Express) 上编译它时没有错误,但在我看来应该有错误。 x 和 y 背后的含义完全不同,因此似乎不应该存在这种隐式转换。那么这是编译器的问题(不太可能),还是我对这里涉及的常量的理解(很可能)。

Possible Duplicate:
Why is my return type meaningless?

Hi, I'm confused about a particular const conversion. I have something like

// Returns a pointer that cannot be modified,   
// although the value it points to can be modified.  
double* const foo()  
{  
    static double bar = 3.14;  
    return &bar;  
}

int main()  
{  
    double* const x = foo(); // fine  
    const double* y = foo(); // eh?!  
    return 0;  
}

When I compile this on MSVS 2008 (Express) there is no error, but it seems to me like there should be. The meaning behind x and y are quite different, so it does not seem like there should be this implicit conversion. So is this an issue with the compiler (unlikely), or my understanding of the const-ness involved here (quite likely).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时光匆匆的小流年 2024-09-23 02:36:55

您所做的相当于以下内容:(

const int a = 5;
int b = a;

您可能知道)这是完全有效的,因为它创建了变量的副本。如果您执行 b = 10a 仍为 5,因此常量不会“被破坏”。

What you did is equivalent to the following:

const int a = 5;
int b = a;

This is (as you probably know) perfectly valid as it creates a copy of the variable. If you do b = 10, a remains 5, so the constness isn't "broken".

却一份温柔 2024-09-23 02:36:55

正如在许多其他问题中多次被问到的那样,从函数返回 const 值是没有意义的。函数返回一个右值,根据定义该右值不能被修改。您可以将其分配给另一个变量这一事实并不令人意外。看看这些问题&有关更多信息的答案:

As has been asked many other times in many other questions, returning a const value from a function is meaningless. A function returns an rvalue, which by definition cannot be modified. The fact that you can assign it to another variable is no real surprise. Check out these questions & answers for more information:

迷迭香的记忆 2024-09-23 02:36:55

返回值无法修改。也就是说,指针无法修改。但是,由于在调用站点,返回值是右值(没有定义的 = 运算符),因此无论如何都无法对其进行修改。

如果返回值是左值(例如引用),则这是可能的:

double* &foo()
{
    static double bar = 3.14;
    double *barP = &bar;
    return barP;
}

double myDouble;
foo() = &myDouble;

但这不可能:

double* const &foo()
{
    static double bar = 3.14;
    double *barP = &bar;
    return barP;
}

double myDouble;
foo() = &myDouble; // error!

const 添加到返回值(至于指针的质量) > as const,而不是指向数据 as const)在你的情况下什么也不做。如果有的话,你的编译器应该警告你这一点,因为如果你只是删除 const 限定符,实际上没有什么不同(除非可能 ABI 更改,尽管我不确定是否在这种情况下,标准允许 ABI 更改)。

The return value cannot be modified. That is, the pointer cannot be modified. However, because at the call site, the return value is an rvalue (without a defined = operator), it cannot be modified anyway.

If the return value was an lvalue (e.g. a reference), this would be possible:

double* &foo()
{
    static double bar = 3.14;
    double *barP = &bar;
    return barP;
}

double myDouble;
foo() = &myDouble;

But this would not be possible:

double* const &foo()
{
    static double bar = 3.14;
    double *barP = &bar;
    return barP;
}

double myDouble;
foo() = &myDouble; // error!

Adding const to the return value (as to quality the pointer as const, not the pointed to data as const) in your case does nothing. If anything, your compiler should warn you about this, because really there's no different if you just remove the const qualifier (barring possible ABI changes, though I'm not sure if the standard allows for ABI changes in this case).

堇年纸鸢 2024-09-23 02:36:55

您始终可以将一个X const(常量X)分配给变量X——该变量稍后可能会被修改,但是X const保证的是< strong>本身不会被修改,当然,它的任何副本也不会被修改。在您的情况下,X 是指针类型,但这不会改变此规则。

您始终可以从 X* 值分配一个 const X* 变量:所指向的数据不会通过您分配的指针变量进行修改 (这就是 its const 的意思),但当然它们可以通过其他途径进行修改。

因此,在这种情况下,您同时执行两个完全合法的分配:在这种情况下,没有理由认为两个合法事物的组合应该是非法的,因为无论如何都没有违反明确的约束。

You can always assign an X const, a constant X, to a variable X -- the variable may later be modified, perhaps, but what an X const guarantees is that itself won't be modified, not of course that any copy of it will never be. In your case X is a pointer type, but that does not change this rule.

You can always assign a const X* variable from an X* value: the pointed-to data won't be modified through the pointer variable you have thus assigned (that's what its const means), but of course they may be modified through other pathways.

So in this case you're doing both perfectly legitimate assignments at the same time: there's no reason why the combination of the two legal things should be illegal, in this case, since no expressed constraint is violated anyway.

抱猫软卧 2024-09-23 02:36:55

使用从右到左的规则来理解“foo”的返回类型。 'foo' 基本上返回 a (也通过 cdecl.org 进行交叉验证)

指向双精度的常量指针

“指向 double 的 const 指针”与“指向 const double 的指针”不同。

第一种情况,指针是const,指向的值可以改变。

在第二种情况下,该点可以改变,但指向的值不能改变。

Use the right-left rule to understand the return type of 'foo'. 'foo' basically returns a (also cross validate through cdecl.org)

const pointer to double

A "const pointer to double" is not the same as "pointer to const double".

In the first case, pointer is const, pointed to value can change.

In the second case, the point can change, the pointed to value cannot change.

吃素的狼 2024-09-23 02:36:55

嘿 jdowner,虽然这里的这些答案很可能回答您的问题,但我也强烈建议您阅读这篇关于 常量正确性。它以简单的英语介绍了所有可能的场景及其含义。

Hey jdowner, although these answers here most likely answer your questions, I also highly recommend you read this article regarding const correctness. It goes over all of the possible scenarios and what they mean in plain english.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文