Search code examples
c++variablessyntaxfunction-declarationmost-vexing-parse

Simple trick to turn function declaration into variable definition


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());

Solution

  • 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()));