是否可以使用显式模板参数调用模板化的用户定义转换运算符? [英] Is it possible to call templated user-defined conversion operator with explicit template arguments?

查看:60
本文介绍了是否可以使用显式模板参数调用模板化的用户定义转换运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑以下代码(使用 clang ++ 7.0.0 成功编译,编译器参数为 -std = c ++ 17 -Wall -Wextra -Werror -pedantic-errors ):

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 :

模板专业化可以通过 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

如您所见,没有 conversion-function-id

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

template-id 语法中提到的

.

mentioned in the template-id grammar.

这篇关于是否可以使用显式模板参数调用模板化的用户定义转换运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆