There is a post here asking for what I believe is equivalent for my purposes (the address of the function as opposed to the type) but the question is already 5 years old; the sole answer states that there might not be a proper solution - without suggesting a particular reason - and offers a partial solution requiring a new function explicitly calling each ADL function in question just to leverage lambda semantics.
template <typename T>
void (*get_swap())(T&, T&) {
return [](T& x, T& y) { return swap(x, y); };
}
I could make this somewhat more generic:
template<typename... T>
void (*get_func())(T &&... t) {
return [](T &&... u) { return func(std::forward<T>(u)...); };
}
Obviously func
ought to be an argument as well - potentially a template argument - but this starts to break down.
template<typename S, typename... T>
void (*get_func())(S (*func)(T &&...), T &&... t) {
return [](T &&... u) { return func(std::forward<T>(u)...); };
}
The answer proceeds to use get_swap
like so:
auto f1 = get_swap<a::b>();
It seems like this is the end of the road: f1
is typed by an explicit instantiation of a function; ADL is out the window.
Consider std::make_tuple
: you already know that std::make_tuple<T...>
is callable on T &&... t
and that calling std::make_tuple(std::forward<T>(t)...)
is equivalent to explicitly instantiating it on T...
. What if you didn't know that? Maybe some of the template arguments are values, or the arguments are packed somehow, or the best candidate happens to include some extra arguments with defaults. You could probably make an argument for RTFM, or for waiting for some kind of reflection concept to come down the pipeline, but I think it's a valid question; obviously the compiler knows the address and type of the function it resolves via ADL. If I were adding this as a feature, I wouldn't know where to begin in terms of syntax (although I would expect it to follow declval
and decltype
as perhaps declfunc
, returning the type of the outermost call.)
In a nutshell, what I'm looking for is a way to ask, "what would you call if I supplied these arguments?" and/or "what is the type of what you would call (...)".
Meta: I intentionally left out version-specific tags in case a solution might exist in a newer version (or a proposal, etc.) For what it's worth, I'm using C++14 coming from a background of mainly C++11. This is an academic interest, at least for now. Also, I aimed for a neutral tone, but in case it sounded otherwise, I have no intention of slighting the author of the answer I cited.