Search code examples
c++templatesargument-dependent-lookup

ADL without templates


Can one show me an example of ADL without using templates? Never seen something like that. I mean something like here. Specifically I am interested in example in which it leads to some pitfall like in mentioned.

EDIT:

I think Tomalak's answer can be extended to pitfall. Consider this:

namespace dupa {

    class A {
    };

    class B : public A {
    public:
        int c;
        B() {
        }
    };

   void f(B b) {
       printf("f from dupa called\n");
   }
}

void f(dupa::A) {
    printf("f from unnamed namespace called\n");
}


int main()
{   
    dupa::B b;
    f(b);

    return 0;
}

Here we expect that f from unnamed namespace will be called, but instead another one is called.


Solution

  • I can't show you something leading to a pitfall, but I can demonstrate ADL working without templates:

    namespace foo {
       struct T {} lol;
       void f(T) {}
    }
    
    int main() {
       f(foo::lol);
    }
    

    Note that lol's type has to be a class-type; I originally tried with a built-in, as you saw, and it didn't work.