Matthew
Matthew

Reputation: 2792

Can I overload an operator taking a pointer and a non-constructed template object?

I would like to write some code like this:

some_container | foo<args...>;

For concreteness, let's say the RHS is foo<2>.

The tricky part is, foo is something that should be automatically instantiated. IOW, I "fail" if foo<2> has to be explicitly instantiated, or if I have to stick () or {} after it.

If the LHS is a class type, I can accomplish this by making foo a function returning a "tag" type, then providing a templated overload of operator|. However, this doesn't work (see Overload operator| for fixed-size arrays? for the code I am currently using) if the LHS is also a pointer type, as is the case if it is a C-style array.

Is there any way in C++11 (i.e. without using C++14 variable templates) to achieve this syntax?

The result of this expression ultimately needs to be :

bar<decltype(some_container), args...>{some_container}

(so e.g. bar<int (&)[N], 2> for LHS int[N] and RHS foo<2>).

Upvotes: 3

Views: 57

Answers (1)

Davis Herring
Davis Herring

Reputation: 39898

No. The syntax foo<...> can only be a class (or alias) template (which is an invalid expression) or function template (which becomes a function pointer, which you can’t overload for).

Upvotes: 1

Related Questions