Search code examples
c++c++20template-argument-deduction

C++ auto deducing not working in class constructor


I have two classes:

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;
};

When I try to create a multimap instance, I get troubles when I trying to let the compiler deduce the types:

int main() {
    multimap<std::string> mp = {
            {{{"key1", "key2", "key3"}, "description1"}, "value"},
            {{{"key4", "key5", "key6"}, "description1"}, "value2"}
    };
    return EXIT_SUCCESS;
}

The shortest way it works is:

multimap<std::string> mp = std::map<string_keys_management, std::string>{
        {{{"key1", "key2", "key3"}, "description1"}, "value"},
        {{{"key4", "key5", "key6"}, "description1"}, "value2"}
};

Compiler: G++-10.2 C++20.

Is this the expected way, or a compiler issue?
If it does the expected way, which rule supports it?


Solution

  • You are missing one more pair of brackets:

    multimap<std::string> mp = {{
        { { { "key1", "key2", "key3" }, "description1" }, "value"  },
        { { { "key4", "key5", "key6" }, "description1" }, "value2" }
    }};