让我们考虑以下代码(使用 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.
更多推荐
是否可以使用显式模板参数调用模板化的用户定义转换运算符?
发布评论