C++ Logo


Advanced search

Can you call a template conversion operator?

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Fri, 29 Jul 2022 20:14:46 -0400
This question is purely theoretical. Let's say we for some reason have
this type:

struct conv
  template<typename T>
  operator some_type();

It has a conversion operator which is a template. However, the type
the conversion operator converts to is not related to the template
parameter of the function. So if we want to call that function, we
would have to do so explicitly by providing a template parameter.

That would look like this:

test t;
auto temp = t.operator some_type<other_type>();

One problem though. [class.conv.fct]/3 says:

> The conversion-type-id in a conversion-function-id is the longest sequence of tokens that could possibly form a conversion-type-id.

The token sequence "some_type<other_type>" "could possibly form a
conversion-type-id". And therefore, it is taken as an entire type ID.
This means that the template arguments are taken as part of the type
ID, and *not* as arguments to the conversion operator template

So... is there a way to work around this? Can you actually call such a
function, or is this a corner case where C++ allows you to write a
function which cannot be called?

Received on 2022-07-30 00:14:58