简体   繁体   English

为什么 int x{ y = 5 } 可能?

[英]Why is int x{ y = 5 } possible?

int main() {
    int y;
    int x{ y = 5 };
    //x is 5
}

How is this possible, since y = 5 is not a calculable expression?这怎么可能,因为 y = 5 不是可计算的表达式?

Also, why doesn't the compiler or IDE complain about main() not returning an int?另外,为什么编译器或 IDE 不抱怨 main() 没有返回 int?

How is this possible, since y = 5 is not a calculable expression?这怎么可能,因为 y = 5 不是可计算的表达式?

It is an assignment, and assignments yield values, ie the "cv-unqualified type of the left operand", see [expr.ass/3] .它是一个赋值,赋值产生值,即“左操作数的 cv 非限定类型”,参见[expr.ass/3] Hence y = 5 results in y , which is 5 , which is used to initialize x .因此y = 5导致y ,即5 ,用于初始化x

With respect to your second question, see cppreference on main (or [basic.start.main/5] ):关于您的第二个问题,请参阅 cppreference on main (或[basic.start.main/5] ):

The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0; main function 的主体不需要包含return语句:如果控制到达 main 的末尾而没有遇到return语句,则效果为执行return 0; . .

Hence, compiler or IDE warning you about a missing return statement at the end of main would be plain wrong.因此,编译器或 IDE 在main末尾警告您缺少return语句是完全错误的。 Admittedly, the fact that you should always return objects from non- void functions execpt main is kind of... well, for historical reason I guess.诚然,您应该始终从非void函数( execpt mainreturn对象的事实有点……好吧,我猜是出于历史原因。

The operator=() results in a value, which is the value assigned to the variable. operator=()产生一个值,该值是分配给变量的值。 Because of this, it is possible to chain assignments like this:因此,可以像这样链接分配:

int x, y, z;
x = y = z = 1;

I will start from your last question我将从你的最后一个问题开始

Also, why doesn't the compiler or IDE complain about main() not returning an int?另外,为什么编译器或 IDE 不抱怨 main() 没有返回 int?

According to the C++ Standard (6.6.1 main function)根据C++标准(6.6.1主要功能)

5 A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value as the argument. 5 main 中的 return 语句具有离开 main function(销毁具有自动存储持续时间的任何对象)并以返回值作为参数调用 std::exit 的效果。 If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0 (see also 18.3).如果控制从 main 的复合语句的末尾流出,则效果等同于操作数为 0 的返回(另见 18.3)。

And relative to this question相对于这个问题

How is this possible, since y = 5 is not a calculable expression?这怎么可能,因为 y = 5 不是可计算的表达式?

From the C++ Standard (8.18 Assignment and compound assignment operators)来自 C++ 标准(8.18 赋值和复合赋值运算符)

1 The assignment operator (=) and the compound assignment operators all group right-to-left. 1 赋值运算符 (=) 和复合赋值运算符都从右到左分组。 All require a modifiable lvalue as their left operand and return an lvalue referring to the left operand.所有这些都需要一个可修改的左值作为它们的左操作数,并返回一个指向左操作数的左值。

Sp this declaration sp 这个声明

int x{ y = 5 };

can be equivalently split into two statements可以等价地分成两个语句

y = 5;
int x{ y };

Moreover in C++ you can even to make a reference to the variable y the following way此外,在 C++ 中,您甚至可以通过以下方式引用变量 y

int &x{ y = 5 };

Here is a demonstrative program这是一个演示程序

#include <iostream>

int main() 
{
    int y;
    int &x{ y = 5 };    

    std::cout << "y = " << y << '\n';

    x = 10;

    std::cout << "y = " << y << '\n';
}

Its output is它的 output 是

y = 5
y = 10

You may this declaration你可以这个声明

int x{ y = 5 };

rewrite also like重写也喜欢

int x = { y = 5 };

However take into account that there is a difference between these (looking similarly as the above declarations) two declarations.但是请考虑到这两个声明(看起来与上述声明相似)之间存在差异。

auto x{ y = 5 };

and

auto x = { y = 5 };

In the first declaration the variable x has the type int .在第一个声明中,变量x的类型为int In the second declaration the variable x has the type std::initializer_list<int> .在第二个声明中,变量x的类型为std::initializer_list<int>

To make the difference more visible see how the values of the objects are outputted.为了使差异更加明显,请查看对象的值是如何输出的。

#include <iostream>

int main() 
{
    int y;
    auto x1 { y = 5 };  

    std::cout << "x1 = " << x1 << '\n';

    auto x2 = { y = 10 };   

    std::cout << "*x2.begin()= " << *x2.begin() << '\n';

    std::cout << "y = " << y << '\n';

    return 0;
}

The program output is程序 output 是

x1 = 5
*x2.begin()= 10
y = 10

If you take a look at the documentation on cppreference , you'll see that operator=() return a reference to the object that was assigned.如果您查看 有关 cppreference 的文档,您会看到operator=()返回对已分配的 object 的引用。 Therefore, a assignment can be used as an expression that returns the object that was assigned.因此,赋值可以用作返回被赋值的 object 的表达式。

Then, it's just a normal assignment with braces.然后,这只是一个带大括号的正常赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么可以将 void(*)(int x, int y) 传递给期望 void(*)() 的 attachInterrupt? - Why is it possible to pass a void(*)(int x, int y) to attachInterrupt that expects void(*)()? 为什么是((unsigned int)x)* y ==((unsigned int)(x * y)总是如此? - why is ((unsigned int)x) * y == ((unsigned int)(x * y) always true? 运算顺序int(4 * x / y)与int(x /(y / 4)) - Order of operation int(4*x/y) vs int(x/(y/4)) int x; int y; int * ptr; 是不是初始化,对吧? - int x; int y; int *ptr; is not initialization, right? 如果int不是类,为什么int x = int(5)合法? - Why is int x = int(5) legal if int is not a class? QTreeWidget childAt(int x,int y)返回NULL - QTreeWidget childAt(int x, int y) returns NULL 如何使用函数 gotoxy(int x, int y) - How to use function gotoxy(int x, int y) (C ++内存管理)如果我们有一个int x = 1234,而int&y = x ...那么堆栈中y的地址是多少? - (C++ memory management) If we have an int x = 1234, and int &y = x… then what is the address of y on the stack? 访问模板类A中的X和Y,如模板<template <int X,int Y> class> class A; - Accessing X and Y in template class A like in template<template<int X, int Y> class> class A; 为什么 if(++x=++y) 有效而 if(x++=++y) 无效? - why does if(++x=++y) works and if(x++=++y) does not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM