1
typedef unordered_map<string, relationNode*> relationMap;
using relation_entry = relationMap::value_type;

void insertNode(string category, relationNode* node) {
   relation_entry insertPair =
     make_pair<string, relationNode*>(category, node);
}

causes an error of "cannot convert 'category' (type 'std::string(aka std::basic_string(char))') to type 'std::basic_string(char)&&" and an error of "cannot convert 'node' (type 'relationNode*') to type 'relationNode*&&".

I was planning to make the pair then insert it into a unordered_map.

I am using "g++ -g -O0 -Wall -Wextra -std=gnu++11" to compile the code. Any help will be greatly appreciated.

  • 1
    you should use `make_pair(category, node)`, without specifying actual parameters types. `make_pair()` was designed exactly for this – grisha Jul 11 '15 at 08:36

1 Answers1

1

Just write:

   relation_entry insertPair =
     make_pair(category, node);

This will work and be more concise (in fact, that's the reason you use std::make_pair instead of calling the constructor directly in the first place).

You should know that this is a backward-compatibility issue with C++11. Consider this piece of C++98 code (I replaced unordered_map with map and using with typedef):

#include <map>
#include <string>
using namespace std; // just for testing

struct relationNode {};

typedef map<string, relationNode*> relationMap;
typedef relationMap::value_type relation_entry;

void insertNode(string category, relationNode* node) {
   relation_entry insertPair =
     make_pair<string, relationNode*>(category, node);
}

int main() {
}

Go to http://cpp.sh/ and try to compile it. You will see that it compiles fine in C++98 mode but not in C++11 and C++14 modes.

For a detailed explanation of the issue, see C++11 make_pair with specified template parameters doesn't compile

Bottom line: Don't specify redundant type arguments and you'll be fine.

Community
  • 1
  • 1
Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • Just write `relation_entry insertPair(category, node);`. Why should `make_pair` be necessary here? (Or useful? I know it's useful if want to deduce the type of the pair, but we know the type of the destination pair already in this case.) – dyp Jul 11 '15 at 12:27
  • @dyp: Right. But I guess (or hope) the OP's code is just a simplified version of something more complex, where `std::make_pair` is justified. – Christian Hackl Jul 11 '15 at 12:48