Template converting constructor with perfect forwarding

Viewed 58

It is possible to replace the two constructors in this program with a single template constructor with perfect forwarding?

#include <unordered_map>
#include <unordered_set>
#include <string>

using Mymap = std::unordered_map<std::string, std::unordered_set<std::string>>;

class A {
    Mymap n_;
public:
    A(Mymap&& n) 
        : n_{std::move(n)} 
    {}

    A(Mymap& n) 
        : n_{n} 
    {}

    // doesn't compile
    //template <typename T>
    //A(T<Mymap::value_type>&& n)
    //    : n_{std::forward(n)}
    //{}
};

int main() {
    A a1{{{"C", {"A", "B"}}}};

    Mymap m{{{"C", {"A", "B"}}}};
    A a2{m};
}
1 Answers

If Mymap is inexpensive to move, then pass by value is often preferred:

A(Mymap n) : n_(std::move(n)) {}

Otherwise, you have to fall back on the ugly solution:

template <class T, class = std::enable_if_t<std::is_same_v<Mymap, std::decay_t<T>>>>
A(T&& n) : n_(std::forward<T>(n)) {}

(Arguably, std::remove_cvref_t fits the intent better than std::decay_t. You can use it starting in C++20.)

Related