mercredi 30 septembre 2020

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.

Aucun commentaire:

Enregistrer un commentaire