1

I would like to exploit the ADL rules to check for the function in an extra namespace:

Say we have a class X.

class X
 {
 ...
 };

In A call

X x;
f(x);

I'd like the compiler to look into namespace funky, that is until now unrelated to class X. But I don't want to clutter the coded by putting funky::f whenever calling f.

One way to achieve this is to define class X as a template class with an argument coming from namespace funky.

template <typename Fake = funky::someClassFromFunky>
class X
 {
 ...
 };

For a call f(x), now, the compiler will indeed look for funky::f.

Is there a cleaner / simpler way of achieving the same behavior? (In particular, referring to some arbitrary class someClassFromFunky in the declaration of class X is awkward.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
Matt Hurst
  • 11
  • 1

1 Answers1

3

You can import f into your namespace like this:

using funky::f;
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • This is the correct answer. The key thing to note is you can do it so that it at the point of call, not just globally. – Flexo Jul 27 '12 at 13:24
  • My problem is that I have a considerable number of functions from funky, operating on class X, that I would like to use (not just f, but say ~ 15 different functions). Putting a using statement for each of those functions would be pretty annoying. – Matt Hurst Jul 27 '12 at 13:30
  • 1
    @MattHurst: I would suggest doing a using declaration for each one, putting them all together. This way if you are searching for the function name, it is easy to spot that it is being imported from another namespace. – Vaughn Cato Jul 27 '12 at 13:32
  • Also, such using statements would cause a different behavior. With `using`, the compiler would also consider `funky::f` for a call `int y; f(y);`. – Matt Hurst Jul 27 '12 at 13:37
  • 1
    @Matt: However, if `X` is in it's own namespace, and you put the `using` into that namespace, then it's only considered when you call `f(X)` – Dave S Jul 27 '12 at 13:39
  • @MattHurst: It is true that it would consider it, but it wouldn't call it unless the argument types actually matched, which seems like the original point anyway. – Vaughn Cato Jul 27 '12 at 13:39
  • Thanks a lot, your answers are fair solutions. --- @Dave: It doesn't have its own namespace. But it would be possible to add one. --- @Vaughn: Such using statements seem problematic in my case, because the functions in question are for example operators (`*`,`+`, etc) and templated. Combined with some automatic conversions, the using statements might yield pretty opaque results. --- So, is there a way to get the *same* behavior for `class X` as for a template class that has a class from `namespace funky` as its argument? – Matt Hurst Jul 27 '12 at 14:30
  • @MattHurst: I wouldn't expect it to be that bad, but maybe you could give an example. – Vaughn Cato Jul 27 '12 at 14:44