简体   繁体   中英

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

#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

As you can see the following statement is evaluated from left to right and the value of isDone is assigned with the last expression of value false .

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

I would like to know the official syntax name for this statement. I have searched 'period statement' through G and without meaningful hits.

Thank you

You are making use of the comma operator

This operator will evaluate the left expression first, then evaluate the second and use it's result as the result of the expression. It's roughly equivalent to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM