c++namespacesargument-dependent-lookup

Why doesn't argument-dependent lookup work for std::make_tuple?


Writing tie instead of std::tie works here, because of argument-dependent lookup:

std::tuple<int, std::string> tup = std::make_tuple<int, std::string>(1, "a");
int i;
std::string str;
tie(i, str) = tup;

But why does make_tuple require std::?


Solution

  • In order for ADL (Argument-dependent lookup) to work, you need one of the arguments to the in the std namespace.

    This is the case in your call to tie (str is a std::string), but not in the call to make_tuple (neigher 1 nor "a" are).

    You could use ADL by supplying a std::string like this:

    //-----------------------------------------------------------------vvvvvvvvvvv--------
    std::tuple<int, std::string> tup = make_tuple<int, std::string>(1, std::string{ "a" });
    

    Note:
    The code above, which attempts to be closest to your version, requires c++20 (where explicit template arguments don't prevent ADL).
    However - this version which is also shorter works in earlier c++ versions:

    std::tuple<int, std::string> tup = make_tuple(1, std::string{ "a" });