c++语法名称(exprA, exprB, exprC)

C++ syntax name for this (exprA, exprB, exprC)

本文关键字:exprB exprC exprA 语法 c++      更新时间:2023-10-16
#include <iostream>
std::string getMyName(void)
{
  std:: cout << "This is function getMyName" << std::endl;
  return std::string("hello world");
}
std::string getMyName2(void)
{
  std:: cout << "This is function getMyName2" << std::endl;
  return std::string("hello world2");
}
int main(void)
{
  // what is the official name for the following syntax?
  bool isDone = (getMyName(), getMyName2(), false);
  std:: cout << "isDone " << isDone << std::endl;
  return 0;
}
user@ubuntu:~/Documents/C++$ ./period 
This is function getMyName
This is function getMyName2
isDone 0

可以看到,下面的语句从左到右求值,isDone的值被赋值为值false的最后一个表达式。

bool isDone = (getMyName(), getMyName2(), false);

我想知道这个语句的正式语法名称。我在G中搜索了'period statement',没有找到有意义的结果。

谢谢

您正在使用逗号操作符

  • http://en.wikipedia.org/wiki/Comma_operator

该操作符将首先计算左侧表达式,然后计算第二个表达式,并将其结果用作表达式的结果。它大致相当于

getMyName();
getMyName2();
bool isDone = false;