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

三世轮回 提交于 2020-08-20 05:39:10

问题


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>();

回答1:


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




回答2:


I understand that this question is for advanced scenari and is asking if the code is compliant with the standard but let's take a step back here for a second:

Operator conversion is design to define "static-cast-like" operation. It doesn't really make sense to make a static cast depend on another templated type



来源:https://stackoverflow.com/questions/55275983/is-it-possible-to-call-templated-user-defined-conversion-operator-with-explicit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!