简体   繁体   中英

Reduce << operator chain in std::cout

Is there any way to reduce the chain of << operator from the statements like following ?

std::cout << var1 << "!=" << var2;

printf() may be an option but anything else?

Because as the number of the operator << increases so the running time too.

Is it possible to efficiently reduce the << chain ?

No, this is not possible.

The fundamental reason is that operator<<(const char*) and operator<<(int) are both simple. In fact, they're so simple that they're likely to be inlined.

printf(const char* format, ...) on the other hand is a very complex function as it needs to deal with many possible format specifiers, and it also has a variable argument list that it needs to parse.

Any other single function would have the same disadvantage of being so complex it can't be inlined.

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