I want to do something like
template <typename T>
void foo(const T& t) {
IF bar(t) would compile
bar(t);
ELSE
baz(t);
}
I thought that something using enable_if
would do the job here, splitting up foo
into two pieces, but I can't seem to work out the details. What's the simplest way of achieving this?
There are two lookups that are done for the name
bar
. One is the unqualified lookup at the definition context offoo
. The other is argument dependent lookup at each instantiation context (but the result of the lookup at each instantiation context is not allowed to change behavior between two different instantiation contexts).To get the desired behavior, you could go and define a fallback function in a
fallback
namespace that returns some unique typeThe
bar
function will be called if nothing else matches because the ellipsis has worst conversion cost. Now, include that candidates into your function by a using directive offallback
, so thatfallback::bar
is included as candidate into the call tobar
.Now, to see whether a call to
bar
resolves to your function, you will call it, and check whether the return type isflag
. The return type of an otherwise chosen function could be void, so you have to do some comma operator tricks to get around that.If our function was selected then the comma operator invocation will return a reference to
int
. If not or if the selected function returnedvoid
, then the invocation returnsvoid
in turn. Then the next invocation withflag
as second argument will return a type that has sizeof 1 if our fallback was selected, and a sizeof greater 1 (the built-in comma operator will be used becausevoid
is in the mix) if something else was selected.We compare the sizeof and delegate to a struct.
This solution is ambiguous if the existing function has an ellipsis too. But that seems to be rather unlikely. Test using the fallback:
And if a candidate is found using argument dependent lookup
To test unqualified lookup at definition context, let's define the following function above
foo_impl
andfoo
(put the foo_impl template abovefoo
, so they have both the same definition context)Are you not able to use full specialisation here (or overloading) on foo. By say having the function template call bar but for certain types fully specialise it to call baz?
I think litb's solution works, but is overly complex. The reason is that he's introducing a function
fallback::bar(...)
which acts as a "function of last resort", and then goes to great lengths NOT to call it. Why? It seems we have a perfect behavior for it:But as I indicated in a comment to litb's original post, there are many reasons why
bar(t)
could fail to compile, and I'm not certain this solution handles the same cases. It certainly will fail on aprivate bar::bar(T t)
litb has given you a very good answer. However, I wonder whether, given more context, we couldn't come up with something that's less generic, but also less, um, elaborate?
For example, what types can be
T
? Anything? A few types? A very restricted set which you have control over? Some classes you design in conjunction with the functionfoo
? Given the latter, you could simple put something likeinto the types and then switch to different
foo
overloads based on that:Also, can the
bar
/baz
function have just about any signature, is there a somewhat restricted set, or is there just one valid signature? If the latter, litb's (excellent) fallback idea, in conjunction with a meta-function employingsizeof
might be a bit simpler. But this I haven't explored, so it's just a thought.If you're willing to limit yourself to Visual C++, you can use the __if_exists and __if_not_exists statements.
Handy in a pinch, but platform specific.