是否可以使用显式模板参数调用模板化的用户定义转换运算符?

编程入门 行业动态 更新时间:2024-10-10 01:20:06
本文介绍了是否可以使用显式模板参数调用模板化的用户定义转换运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

让我们考虑以下代码(使用 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.

更多推荐

是否可以使用显式模板参数调用模板化的用户定义转换运算符?

本文发布于:2023-11-09 04:03:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   可以使用   运算符   定义   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!