是否可以使用明确的模板参数调用模板的用户定义转换操作员
Is it possible to call templated user-defined conversion operator with explicit template arguments?
让我们考虑以下代码(用 clang 7.0.0 成功编译,编译器参数为 -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;
}
是否可以使用明确提供的模板参数调用这种模板的用户定义的转换操作员?天真的方法没有编译:
const auto i = Foo{}.operator int<bool>();
[temp.names](模板专业名称)/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
如您所见,没有转换 - 函数-ID
conversion-function-id:
operator conversion-type-id
template-id 语法中提到的。所以答案是否定的。不可能。
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- Visual C++(VS2017)中用户定义的转换不明确
- 使用用户定义的参数调用future/async并调用类方法
- 带有用户定义类的c++折叠表达式
- g++用户定义的动态链接库上的全局new和delete运算符
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 修改"std::set"中用户定义类型的值
- 参数包构造函数在类模板中隐藏用户定义的转换
- MAKE:找不到包含的用户定义的头文件?
- C++:用户定义的显式类型转换函数错误
- 从用户定义的类生成格式字符串?
- 为用户定义的类正确调用复制构造函数/赋值运算符
- C++ 向量与用户定义的类比较?(==, <, >)
- STL 用户定义的二进制操作
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 使用宏编译时使用用户定义的数学函数,或者仅使用 c++ 中标准数学库中的函数
- C++:用户定义的类,以成员字段作为地址