Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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::?

like image 964
Blrp Avatar asked Feb 25 '26 15:02

Blrp


1 Answers

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" });
like image 99
wohlstad Avatar answered Feb 27 '26 05:02

wohlstad