Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize function arguments that are classes with default value

I'm working on Linux gcc environment and I need to initilize function arguments that are classes with default values. When I do that with temporary instance of the class it makes an error like this: "default argument for [function argument] has type [class name]. for example:

void foo(std::wstring& str = std::wstring())

error: default argument for 'std::wstring& str' has type 'std::wstring' P.S. this code is compiled without any error or warning with VC++.

How can I initilize the default value?

like image 346
mle977 Avatar asked Nov 21 '10 13:11

mle977


2 Answers

This is supposed to not compile. You are trying to bind an rvalue to a non-const reference. Say std::wstring const & str and it should work.

like image 83
dennycrane Avatar answered Oct 23 '22 16:10

dennycrane


You could just create a function overload:

void foo() {
    std::wstring str;
    foo(str);
}

but I really miss the point.

EDIT: I mean, that function's purpose is almost certainly to modify an input string. If you provide an empty input string that you can't access later, why bother?

like image 27
Simone Avatar answered Oct 23 '22 15:10

Simone