C++ C++;自动推断在类构造函数中不起作用
我有两门课:C++ C++;自动推断在类构造函数中不起作用,c++,c++20,template-argument-deduction,C++,C++20,Template Argument Deduction,我有两门课: class string_keys_management { public: string_keys_management(const string_keys_management&) = default; string_keys_management(string_keys_management&&) = default; string_keys_management& operator=(const string_keys
class string_keys_management {
public:
string_keys_management(const string_keys_management&) = default;
string_keys_management(string_keys_management&&) = default;
string_keys_management& operator=(const string_keys_management&) = default;
string_keys_management& operator=(string_keys_management&&) = default;
string_keys_management(const std::initializer_list<std::string>& keys, const std::string &description = "") : keys(keys), description(description) {}
bool operator==(const std::string &ref) { /*...*/ }
bool operator!=(const std::string &ref) { /*...*/ }
bool operator<(const string_keys_management &ref) const { /*...*/ }
private:
std::vector<std::string> keys;
std::string description;
};
template <typename V>
class multimap {
public:
multimap(const std::map<string_keys_management, V> &ref) : values(ref) {}
multimap& operator=(const std::map<string_keys_management, V> &ref) { values = ref; }
V& at(const std::string &key) { return values.at(key); }
void insert(const string_keys_management &keys, V val) { values.insert({keys, val}); }
private:
std::map<string_keys_management, V> values;
};
最短的工作方式是:
multimap<std::string> mp = std::map<string_keys_management, std::string>{
{{{"key1", "key2", "key3"}, "description1"}, "value"},
{{{"key4", "key5", "key6"}, "description1"}, "value2"}
};
multimap mp=std::map{
{{{“key1”,“key2”,“key3”},“description1”},“value”},
{{{“key4”、“key5”、“key6”},“description1”},“value2”}
};
编译器:G++-10.2c++20
这是预期的方式,还是编译器问题?如果它按预期的方式执行,则哪个规则支持它?您缺少一对括号:
multimap mp={{
{{{“key1”,“key2”,“key3”},“description1”},“value”},
{{{“key4”、“key5”、“key6”},“description1”},“value2”}
}};
您又少了一对括号:
multimap mp={{
{{{“key1”,“key2”,“key3”},“description1”},“value”},
{{{“key4”、“key5”、“key6”},“description1”},“value2”}
}};
multimap<std::string> mp = std::map<string_keys_management, std::string>{
{{{"key1", "key2", "key3"}, "description1"}, "value"},
{{{"key4", "key5", "key6"}, "description1"}, "value2"}
};