如何隐式调用函数调用运算符
How to Implicitly Call a Function Call Operator?
如何隐式调用类对象的模板化函数调用运算符?
class User_Type
{
public:
template< typename T > T operator()() const;
};
void function()
{
User_Type user_var;
int int_var_0 = user_var.operator()< int >(); // explicit function call operator; ugly.
int int_var_1 = user_var< int >(); // implicit function call operator.
}
g++-4.9 -Wall -Wextra
的输出错误为:
error: expected primary-expression before ‘int’
auto int_var_1 = user_var< int >();
^
如果需要显式指定模板参数,则不能。指定它的唯一方法是使用第一个语法。
如果模板参数可以从函数参数推导出来,或者有默认值,那么如果需要该参数,可以使用简单的函数调用语法。
根据类型的用途,使类(而不是成员)成为模板可能是有意义的,因此您可以在声明对象时指定参数。
没有办法。使用像这样未使用的参数可以作弊
class User_Type
{
public:
template< typename T > T operator()( T * ) const;
};
void function()
{
User_Type user_var;
int int_var_1 = user_var((int*)0); // implicit function call operator.
}
但这并不是你想要的。
相关文章:
- C++ 如何重载 [] 运算符并进行函数调用
- 在 C++ 中调用类中的运算符函数
- NOEXCEPT 函数调用运算符的说明符_Not_fn
- 检查类是否具有可能重载的函数调用运算符
- C++函数调用与许多点运算符
- C++ 类的构造函数和函数调用运算符 () 重载之间的歧义
- 重载运算符主体仅包含一个函数调用
- 重载<<运算符的函数调用 (C++)
- 使用赋值运算符将宏转换为函数调用
- 为什么将"<<"运算符作为函数调用会导致"ambiguous call"编译器错误?
- 如何调用运算符函数添加两个对象?
- 将函数调用替换为重载运算符 C++
- 在呼叫运算符函数const中调用运算符时错误
- 运算符 () 括号是否会在函数调用中创建的对象上调用
- 当您可以使用构造函数时,为什么要重载 C++ 类或结构中的 () 运算符(可调用运算符)
- 重载时获取堆栈溢出 >> 运算符函数是为类调用的。我需要进行哪些更改?
- 内置运算符(新的,删除和函数调用除外)如何返回
- 为下标运算符和函数调用运算符提供默认参数
- 使用重载运算符在函数调用时执行操作
- 流输出和隐式void*cast运算符函数调用