Constructor
Constructor

Reputation: 7473

Is it possible to call templated user-defined conversion operator with explicit template arguments?

Lets consider the following code (compiles successfully with clang++ 7.0.0, compiler arguments are -std=c++17 -Wall -Wextra -Werror -pedantic-errors):

#include <iostream>


struct Foo
{
    template <typename Type = void>
    operator int()
    {
        return 42;
    }
};


int main()
{
    const auto i = Foo{}.operator int();

    std::cout << i << std::endl;
}

Is it possible to call such templated user-defined conversion operator with explicitly provided template arguments? The naive approach doesn't compile:

const auto i = Foo{}.operator int<bool>();

Upvotes: 2

Views: 228

Answers (1)

Language Lawyer
Language Lawyer

Reputation: 3569

[temp.names](Names of template specializations)/1:

A template specialization can be referred to by a template-id:

simple-template-id:
  template-name < template-argument-listₒₚₜ >

template-id:
  simple-template-id
  operator-function-id < template-argument-listₒₚₜ >
  literal-operator-id < template-argument-listₒₚₜ >

template-name:
  identifier

As you can see, there is no conversion-function-id

conversion-function-id:
    operator conversion-type-id

mentioned in the template-id grammar. So the answer is no. It is not possible.

Upvotes: 6

Related Questions