C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources]

Herb Sutter, Andrei Alexandrescu

نسخه متنی -صفحه : 521/ 167
نمايش فراداده

Examples

Example: Initialization library with overloaded

operator,

for sequence initialization. One library helpfully tried to make it easier to add multiple values to a container in one shot by overloading the comma. For example, to append to a

vector<string> letters :

set_cont(letters) += "a", "b";

// problematic

That's fine until the day the caller writes:

set_cont(letters) += getstr(), getstr();

// order unspecified when using overloaded ","

If

getstr gets user console input, for example, and the user enters the strings

"c" and

"d" in that order, the strings can actually be applied in either order. That's a surprise, because this is not a problem for the built-in sequencing

operator, :

string s = getstr(), getstr();

// order well-specified using built-in ","