简体   繁体   中英

How to assign a default (empty) value to the reference of a std::unordered_map in the declaration of a function?

I have a question, I've been doing some googling but I can't find a solution (yet).

I have a function that may / or may not receive a value of type std::unordered_map , as I indicate, on some occasions this std::unordered_map will be received empty and no processing of any kind will be executed on it, to achieve this, I assigned a value by default in declaration, like this:

bool existence_checker(const std::string&, std::unordered_map<std::string,std::string> = {});

Declaring the function as indicated does not present any errors, since as far as I know (and would like opinions), a std::unordered_map can be assigned as empty in the same way as a std::vector .

The problem arises when I want to assign a default value to the reference of the std::unordered_map , I have done some tests with some answers found in google, but have not been successful. Something like this:

bool existence_checker(const std::string&, std::unordered_map<std::string,std::string>& = {});

The error it throws is: error: could not convert ' ()' from '' to 'std :: unordered_map <std :: __ cxx11 :: basic_string , std :: __ cxx11 :: basic_string > & '|

In short: How can I assign a default (empty) value to the reference of a std::unordered_map in a function declaration?

PD. Thanks in advance and sorry for the bad English.

I have a function that may / or may not receive a value of type std::unordered_map [...]

As your function may or may not receive an argument for a given parameter, you may want to consider changing the interface of the function to take a std::optional second parameter, which wraps you std::unordered_map type.

#include <optional>
#include <string>
#include <unordered_map>

using MyUMap = std::unordered_map<std::string, std::string>;
bool existence_checker(const std::string& s = "", 
                       const std::optional<MyUMap>& um = std::nullopt);

Note that the particular problem in your example is that the second parameter is that of a non-const reference, an rvalue objects cannot bind to non-const lvalue references . Ie, if you want to provide a default argument for the second parameter, you will need (*) to make that parameter const& .

using MyUMap = std::unordered_map<std::string, std::string>;
bool existence_checker(const std::string& s = "", 
                       const MyUMap& um = {});

From the comments to the original posts, it is apparent that the API design of the free function is intended to a non-const reference that may or may not be modified , and a user should be able to leverage a default argument for the possibly modified in-out parameter. This looks very much like a C-style function API, and could be realized by passing a pointer an std::unordered_map object, where the pointer may be std::nullptr , and where the default argument for the pointer parameter is set to std::nullptr .

Instead of C-style API:s, you may want to consider either overloading the function for the cases of

I need an unordered map and will modify it

and

I don't need a map and will not modify it

or, wrapping the std::unordered_map object in a custom type where existence_checker API is a non-static member function (that may or may not modify the std::unordered_map non-static data member).


(*) You could use a default argument for a non-const reference parameter, but you would need to refer to an object of static storage duration, and this would be quite a weird pattern, and likely not what you were intending:

using MyUMap = std::unordered_map<std::string, std::string>;
MyUMap default_map_object_with_static_storage_duration{};

bool existence_checker(const std::string& s = "", 
                       MyUMap& um = default_map_object_with_static_storage_duration);

How can I assign a default (empty) value to the reference of a std::unordered_map in a function declaration?

Write an overload that has a local empty map. Otherwise you might mutate that call site's default, then in later calls it isn't empty.

bool existence_checker(const std::string& str = "")
{
    std::unordered_map<std::string,std::string> empty;
    return existence_checker(str, empty);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM