0

Is this code snippet portable?

definition:

#define log(...)  std::cout << __FILE__ << "[" << __LINE__  << "]:" \
                            << string_format(__VA_ARGS__) \
                            << std::endl << std::flush

usage:

log("i = %d", i);

and string_format is sprintf that outputs a std::string taken from https://stackoverflow.com/a/8098080/624074 It works with my gcc 4.6.3 here but I don't want to have compilation issues later with other compilers.

Community
  • 1
  • 1
Mohammad Moghimi
  • 4,636
  • 14
  • 50
  • 76

2 Answers2

2

Yes, the macro __VA_ARGS__ is part of standard C++:

C++11 ยง16.3.1 Argument substitution

An identifier __VA_ARGS__that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing tokens used to replace it.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
2

It's pretty portable, for the most part. It's part of the C++11 language standard (as well as C99), so most modern compilers support it. You might run into problems with older compilers that only support C++03.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589