0

I know that the following statement is interpreted as a function declaration instead of a variable definition

boost::system::system_error sys_err(boost::system::error_code());

Is there any simple trick to turn it into a one-liner variable definition which is what I intend? I don't quite like the writing

boost::system::system_error sys_err(0, boost::system::system_category());
Lingxi
  • 14,579
  • 2
  • 37
  • 93
  • 4
    `std::system_error sys_err{std::error_code()};` – Dúthomhas Nov 14 '15 at 17:27
  • @Dúthomhas You're correct. My fault not to make my intention clear. I'm actually using Boost, and I don't want my code to have a dependency on C++11. – Lingxi Nov 14 '15 at 17:36
  • 2
    The way compilers interpret this ambiguous syntax is called "most vexing parse". On SO we even have a tag for it. If you read the [tag's description](http://stackoverflow.com/tags/most-vexing-parse/info) you can even find the answer to your question! :-) – Fabio says Reinstate Monica Nov 14 '15 at 18:22

1 Answers1

3

Dúthomhas' answer will do, provided your compiler supports at least C++11. For a more universal solution you can wrap the argument in a extra pair of parentheses: boost::system::system_error sys_err((boost::system::error_code()));

Alberto M
  • 1,057
  • 8
  • 24