c++ - constructing a pair(string, *node) to insert into an unordered_map -


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 error of "cannot convert 'category' (type 'std::string(aka std::basic_string(char))') type 'std::basic_string(char)&&" , error of "cannot convert 'node' (type 'relationnode*') type 'relationnode*&&".

i planning make pair insert unordered_map.

i using "g++ -g -o0 -wall -wextra -std=gnu++11" compile code. appreciated.

just write:

   relation_entry insertpair =      make_pair(category, node); 

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

you should know backward-compatibility issue c++11. consider piece of c++98 code (i replaced unordered_map map , using typedef):

#include <map> #include <string> using namespace std; // 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 http://cpp.sh/ , try compile it. see compiles fine in c++98 mode not in c++11 , c++14 modes.

for detailed explanation of issue, see c++11 make_pair specified template parameters doesn't compile

bottom line: don't specify redundant type arguments , you'll fine.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -