Default parameter : std::map

1
2
3
4
5
6
7
8
9
10
11
#include <string>
#include <map>

class MyClass {
private:
	std::map<std::string, std::string> map_;
public:
	MyClass( const std::map<std::string, std::string>& default_map = std::map<std::string, std::string> ) : map_(default_map)
	{}
//....
};


When I try to compile this with gcc, it says two errors to the constructor:
- error: expected `,' or `...' before '>' token
- error: wrong number of template arguments (1, should be 4)


What is the problem?
add parentheses for the default value:
MyClass( const std::map<std::string, std::string>& default_map = std::map<std::string, std::string>() )
i dont think if this is possible. In my view the reason is you are taking a reference and then you assigning something which doesnt exist!!

1
2
3
MyClass( const std::map<std::string, std::string>& default_map = class="quote">
class="qd">std::map<std::string, std::string>
) : \ map_(default_map) {}


the commented thing doesnt exist actually and hence how you can take a reference of it.

what we can do is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MyClass {
private:
        std::map<std::string, std::string> map_;
public:
        MyClass(const map<string, string> default_map = mk_map()) : map_(default_map)
        {       }

        static map<string, string> mk_map()
        {
                map<string, string>  m;
                m.insert(pair<string, string>("",""));
                return m;
        }
};


@Bazzy
add parentheses for the default value:

Of course I tried it with parentheses , and without const reference too, but it doesn't worked either way.

@writeonsharma
It seems that only std::map has problems like this (is there a specific reason for this?), but I could solve the problem by adding an empty constructor

Thanks for your help
yes you can.. i thought you want only this kind of code so i wrote for you. other wise you can remove the default value and the code will run fine.

there is some problem in maps when we initialize them in initialization list.. since some time and im trying to figure it out but with no success. this is related to the rest of two parameters of map which are compare and allocator. we need to give them also some values but how im not able to find.
It is totally valid to do what Bazzy suggested.

jsmith:

what bazzy suggested i've tried that and its giving error. even if it will compile it will give segmentation fault. how can you take a reference of nothing? am i correct? you can try it.
It is a temporary. You can bind const references to temporaries. It is exactly the same as

 
MyClass c( std::map<std::string, std::string>() );


You try it.

From stl_vector.hpp:

1
2
3
      explicit
      vector(const allocator_type& __a = allocator_type())
      : _Base(__a) { }


From stl_map.hpp:

1
2
3
    explicit
    map(const _Compare& __comp, const allocator_type& __a = allocator_type())
      : _M_t(__comp, __a) { }


From stl_deque.hpp:

1
2
3
4
 
   explicit
    deque(const allocator_type& __a = allocator_type())
      : _Base(__a, 0) {}


Etc, etc, etc.


Last edited on
My apologies for not seeing the declaration properly... I take your point smith :)

But the main problem still remains, that the constructor is not working and what Bazzy suggested is not correct. he wrote this:

MyClass( const std::map<std::string, std::string>& default_map = std::map<std::string, std::string>() )

He is correct. Notice how the STL does the same thing (ie, bind temporary to const reference).

What Bazzy did has always worked. There must be a bug elsewhere.
you talking about a bug in gcc... :D hehehe.. anyway..

1
2
3
4
5
6
7
8
class MyClass {
private:
        std::map<std::string, std::string> map_;
public:
        MyClass( const std::map<std::string, std::string>& default_map = std::map<std::string, std::string>()) : map_(default_map)
        {}
//....
};


Im trying to compile this code..hope i'm not missing something stupid!!!!!
Topic archived. No new replies allowed.