C++ Logo

std-discussion

Advanced search

Re: Can you call a template conversion operator?

From: Brian Bi <bbi5291_at_[hidden]>
Date: Fri, 29 Jul 2022 20:45:42 -0400
On Fri, Jul 29, 2022 at 8:15 PM Jason McKesson via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> 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
> function.
>

I think this is incorrect; some_type<other_type> cannot form a
conversion-type-id because some_type<other_type> is not a valid
type-specifier. A simple-template-id is a type-specifier, but takes the
form template-name<template-argument-list(opt)>. Here, some_type is not a
template-name, because there isn't any template named some_type.

However, it appears that you're correct that there is no syntax that allows
specifying explicit template arguments to a conversion function. The syntax
of template-id <http://eel.is/c++draft/temp.names#nt:template-id>does not
permit it.


>
> 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?
>

I would argue that the work-around is to simply not declare such a thing.
You obviously aren't intending to use it the way that a conversion function
is typically used (*i.e.*, for an implicit or explicit conversion), so you
don't need it to be a conversion function in the first place.

-- 
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>
-- 
*Brian Bi*

Received on 2022-07-30 00:45:54