stl container as default arguments - how?

F

FBergemann

Hi,

i wanted to merge two constructors

explicit Tokenizer (const string& token);
explicit Tokenizer (const string& token, const list<string>&
elements);

into one

explicit Tokenizer (const string& token, const list<string>&
elements = const list<string>& ());

But this doesn't compile.
What's the correct syntax?

- Thanks!

Frank
 
R

Rolf Magnus

Hi,

i wanted to merge two constructors

explicit Tokenizer (const string& token);
explicit Tokenizer (const string& token, const list<string>&
elements);

into one

explicit Tokenizer (const string& token, const list<string>&
elements = const list<string>& ());

But this doesn't compile.
What's the correct syntax?

You can't default-initialize a reference.

explicit Tokenizer (const string& token,
const list<string>& elements = list<string> ());

Note that this will create a temporary empty list whenever the default
argument is used.
 
F

FBergemann

Rolf Magnus schrieb:
[...]
thanks for the hint - but you're right:
Note that this will create a temporary empty list whenever the default
argument is used.

that's a reason to hold on two constructors for it

Regards!

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top