Possible Duplicate:
how to initialize function arguments that are classes with default value
#include <string>
void foo1(const std::string& s = std::string());
void foo2(std::string& s = std::string());
void foo3(const std::string s = std::string());
void foo4(std::string s = std::string());
error at foo2(): default argument for ‘std::string& s’ has type ‘std::string {aka std::basic_string<char>}’
I understand the compiler’s point, but I don’t get how this does not apply to foo1() as well.
You can’t take a non-const reference to a temporary like foo2 does.
Notice that this isn’t specifically default parameters. You get the same error for function variables: http://ideone.com/g7Tf7L
When you take a const reference to a temporary, the lifetime of the temporary is extended to the lifetime of the reference (§12.2, quoted from my copy of C++11 draft n3337):