5

I want to consider functions in the namespace of non-type template parameters during lookup in a function call.

For example:

template<auto& N>
struct test_nontype {
    int call() {
        return f(*this);  // error: 'f' was not declared in this scope
    }
};

template<typename T>
struct test_type {
    int call() {
        return f(*this);  // ok, f is found in ns via ADL
    }
};


namespace ns {
    static struct type_in_ns {} value;
    int f(test_nontype<value>) {
        return 3;
    }
    int f(test_type<type_in_ns&>) {
        return -3;
    }
}

int main() {
    int x = test_type<ns::type_in_ns&>{}.call();
    return x + test_nontype<ns::value>{}.call();
}

With types it works fine, but the last line fails to compile since int ns::f(test_nontype<ns::value>) can't be found.

I would rather not add a parameter template<auto& N, typename = decltype(N)> struct test_nontype { if there are alternatives, since adding a template arguments and that just makes the interface more confusing (I would have to add a comment explaining it's for ADL, but the IDE still suggests adding a type name, and it also adds more noise to compile errors)

What other work arounds are there?

Artyer
  • 31,034
  • 3
  • 47
  • 75
  • Can you add the exact error message? – cigien Oct 27 '20 at 23:25
  • @cigien `error: 'f' was not declared in this scope` pointing at `f(*this)` in `test_nontype` – Artyer Oct 27 '20 at 23:30
  • 1
    Thanks. It should be added to the question though, e.g. like I edited. – cigien Oct 27 '20 at 23:31
  • I see that you're asking for alternatives. What's wrong with your solution? – cigien Oct 28 '20 at 00:05
  • @cigien I have to add a template arguments and that just makes the interface more confusing (Have to add a commend explaining it's for ADL, but IDE still suggests autocomplete adding a typename, and it also adds more noise to compile errors) – Artyer Oct 28 '20 at 00:09
  • Hmm, I see your point. But I think that's the best you can do to trigger ADL. Of course, that's only the limit of my imagination :) Also, please add that motivation for an alternative to the question. – cigien Oct 28 '20 at 00:15

0 Answers0