8
void doStuff( std::string const & s1, std::string const & s2="");

I was wondering if this code is legal in C++, for the s2 string. I want to have a default argument, but passing a reference and having an empty string as default. Will a temporary be created, and the reference will point to that temporary, or is it illegal C++?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431

3 Answers3

15

Yes this is legal. const will ensure temporary be lasting till function doStuff finishes.

§ 12.2.5

A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.

billz
  • 44,644
  • 9
  • 83
  • 100
4

Better will be

void doStuff( std::string const & s1, std::string const & s2 = std::string());

To avoid extra temporary const char *. (Your variant has 2 temporaries: a const char * and empty std::string).

Or, using user-defined literals (C++14):

void doStuff( std::string const & s1, std::string const & s2 = ""s);
vladon
  • 8,158
  • 2
  • 47
  • 91
2

That to understand the semantic it is better to split the original statemenet .

void doStuff( std::string const & s1, std::string const & s2="");

into two statements

void doStuff( std::string const & s1, std::string const & s2);
doStuff( SomeString, "" );

In the call of the function the second argument is implicitly converted to an object of type std::string:

s2 = std::string( "" );

So in fact in the body of the function you will have

std::string const &s2 = std::string( "" );

That is constant reference s2 will refer temporary object std::string( "" ).

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335