samedi 25 juillet 2020

Can C++'s conditional operator ?: use {} instead of std::make_pair? [duplicate]

This compiles: condition ? std::make_pair(1,2) : std::make_pair(3,4).

This doesn't: condition ? {1,2} : {3,4}.

At the first {, g++ says "expected primary-expression before '{' token." Even when you enclose the braces in parentheses, the braces are interpreted as a block of statements, not as a std::pair. This is with Ubuntu 18's g++ 7.5, either with its default -std=c++14 or with its older -std=c++11.

In a ?: expression, can one use this abbreviation for make_pair that C++11 introduced? If so, how?

Aucun commentaire:

Enregistrer un commentaire