C++ 自动推导在 class 构造函数中不起作用
C++ auto deducing not working in class constructor
我有两个类:
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
实例时,当我试图让编译器推导类型时遇到了麻烦:
int main() {
multimap<std::string> mp = {
{{{"key1", "key2", "key3"}, "description1"}, "value"},
{{{"key4", "key5", "key6"}, "description1"}, "value2"}
};
return EXIT_SUCCESS;
}
最短的工作方式是:
multimap<std::string> mp = std::map<string_keys_management, std::string>{
{{{"key1", "key2", "key3"}, "description1"}, "value"},
{{{"key4", "key5", "key6"}, "description1"}, "value2"}
};
编译器:G++-10.2 C++20.
这是预期的方式,还是编译器问题?
如果它按预期方式运行,哪个规则支持它?
您还缺少一对括号:
multimap<std::string> mp = {{
{ { { "key1", "key2", "key3" }, "description1" }, "value" },
{ { { "key4", "key5", "key6" }, "description1" }, "value2" }
}};
我有两个类:
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
实例时,当我试图让编译器推导类型时遇到了麻烦:
int main() {
multimap<std::string> mp = {
{{{"key1", "key2", "key3"}, "description1"}, "value"},
{{{"key4", "key5", "key6"}, "description1"}, "value2"}
};
return EXIT_SUCCESS;
}
最短的工作方式是:
multimap<std::string> mp = std::map<string_keys_management, std::string>{
{{{"key1", "key2", "key3"}, "description1"}, "value"},
{{{"key4", "key5", "key6"}, "description1"}, "value2"}
};
编译器:G++-10.2 C++20.
这是预期的方式,还是编译器问题?
如果它按预期方式运行,哪个规则支持它?
您还缺少一对括号:
multimap<std::string> mp = {{
{ { { "key1", "key2", "key3" }, "description1" }, "value" },
{ { { "key4", "key5", "key6" }, "description1" }, "value2" }
}};