I haven't been able to figure this out. It's easy to create two ctors but I wanted to learn if there's an easy way to do this.
How can one pass a std::map
as the default parameter to a ctor, e.g.
Foo::Foo( int arg1, int arg2, const std::map<std::string, std::string> = VAL)
I've tried 0
, null
, and NULL
as VAL
, none of the work because they are all of type int, g++ complains. What is the correct default to use here?
Or is this kind of thing not a good idea?
First, and tangentailly, you are passing the map by const value, which is pointless and probably not what you really want. You probably want to pass by const reference, so that you don't make a copy of the map, and you ensure that your function doesn't modify the map.
Now, if you want your default parameter to be an empty map, you do it by constructing it, like so:
Since C++11 you can use aggregate initialization:
Example:
You can play around with this example at Godbolt.
You create a value-initialized temporary. For example:
(A typedef might help to make this more readable in your code)
The correct expression for
VAL
isstd::map<std::string, std::string>()
. I think that looks long and ugly, so I'd probably add a public typedef member to the class:And by the way, did you mean for the last constructor argument to be a reference?
const map_type&
is probably better than justconst map_type
.