如何隐式调用类对象的模板化函数调用运算符?

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 >();
                                   ^

最佳答案

如果需要显式指定template参数,则不需要。指定它的唯一方法是使用您的第一种语法。

如果模板参数可以从函数参数推导出或具有默认值,那么如果需要该参数,则可以使用简单的函数调用语法。

根据类型的用途,有可能使类而不是成员成为模板,因此可以在声明对象时指定自变量。

09-10 04:48
查看更多