Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass std::map as a default constructor parameter in c++ class function

Tags:

c++

stdmap

clang

I have a problem when attempting to use std::map in clang-3.3 and clang-3.0 on Ubuntu 12.04:

#include <iostream>
#include <map>
#include <string>

class A
{
public:
#if 0 //clang compiles ok
    typedef std::map<std::string,std::string> MapKeyValue_t;
    void PrintMap(const MapKeyValue_t &my_map 
        = MapKeyValue_t())
#else // clang compiles fail
    void PrintMap(const std::map<std::string,std::string> &my_map 
    = std::map<std::string,std::string>())
#endif
{
    std::map<std::string,std::string>::const_iterator it;
    for (it = my_map.begin(); it != my_map.end(); it++)
    {
        std::cout << it->first << " " << it->second << std::endl;
    }
}
};

int main()
{
    A a;
    a.PrintMap();
    return 0;
}

However, while the code compiles in both g++ and clang I keep getting these errors as output:

test.cpp:14:36: error: expected ')'
        = std::map<std::string,std::string>())
                                          ^
test.cpp:13:15: note: to match this '('
        void PrintMap(const std::map<std::string,std::string> &my_map 
                     ^
test.cpp:14:24: error: expected '>'
        = std::map<std::string,std::string>())
                              ^
test.cpp:28:13: error: too few arguments to function call, expected 2, have 0
        a.PrintMap();
        ~~~~~~~~~~ ^
test.cpp:13:2: note: 'PrintMap' declared here
        void PrintMap(const std::map<std::string,std::string> &my_map 
        ^
3 errors generated.

The closest thing I could find that matches my problem is this topic: How to pass std::map as a default constructor parameter

But, I have no idea what's wrong. Hopefully, someone can shed some light on this, please.

Update:

void PrintMap(const std::map<std::string,std::string> &my_map 
        = (std::map<std::string,std::string>()))

is ok. Thanks.

like image 923
xiaoyur347 Avatar asked Aug 10 '13 09:08

xiaoyur347


People also ask

How do you pass a parameter to a constructor in C++?

Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function.

Can default constructor have parameters?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Under what circumstances will C++ provide a default constructor for a class?

If no user-declared constructors of any kind are provided for a class type (struct, class, or union), the compiler will always declare a default constructor as an inline public member of its class.

What is a default constructor It is a constructor always accept parameters?

A constructor that takes no parameters (or has parameters that all have default values) is called a default constructor. The default constructor is called if no user-provided initialization values are provided. This class was designed to hold a fractional value as an integer numerator and denominator.


2 Answers

I compiled and run it successfully in vs2012.
So I think it's compilers problem.

like image 55
BlackMamba Avatar answered Sep 30 '22 19:09

BlackMamba


The other posters are correct, I think this is an instance of Bug 13657 which should be fixed in Clang 3.4.

As mentioned in the bug report and the C++ Standard Core Language Active Issues page linked from there (and as you mentioned in your update), you can work around the issue by adding parentheses to the default value as follows:

void PrintMap(const std::map<std::string,std::string> &my_map 
    = (std::map<std::string,std::string>()))
like image 37
Kevinoid Avatar answered Sep 30 '22 20:09

Kevinoid