我可以在不实例化的情况下使用函数对象吗?

发布于 2025-01-03 11:15:02 字数 823 浏览 1 评论 0原文

有以下代码:

template<typename T, typename OutStream = std::ostream> struct print {
  OutStream &operator()(T const &toPrint, OutStream &outStream = std::cout) const {
    outStream << toPrint;
    return outStream;
  }
};

此调用是错误的:

print<int>(2);

错误消息:

1>main.cpp(38): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'print<T>'
1>          with
1>          [
1>              T=int
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

此调用不是错误的:

print<int> intPrinter;
intPrinter(2);

我可以在没有实例化的情况下以某种方式使用函数对象吗? 我不能在这里使用模板函数,因为我需要部分专业化功能。

Having the following code:

template<typename T, typename OutStream = std::ostream> struct print {
  OutStream &operator()(T const &toPrint, OutStream &outStream = std::cout) const {
    outStream << toPrint;
    return outStream;
  }
};

This call is erroneous:

print<int>(2);

Error message:

1>main.cpp(38): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'print<T>'
1>          with
1>          [
1>              T=int
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous

This call is not erroneous:

print<int> intPrinter;
intPrinter(2);

Can I use a function object somehow without its instantiation?
I cannot use a template function here, because I need partial specialization capabilities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒与心事 2025-01-10 11:15:02

我认为你想说

print<int>()(2);

这里,第一个括号通过调用(零参数)构造函数创建一个临时 print 对象,然后第二个括号实际上调用该对象上的函数调用运算符。您现在遇到的错误是由于

print<int>(2);

Is 解释为将 2 转换为 print 的类型转换表达式,这不是您想要的(而且也不合法) )。

希望这有帮助!

I think that you want to say

print<int>()(2);

Here, the first parens create a temporary print<int> object by calling the (zero-argument) constructor, then the second parens actually invoke the function call operator on that object. The error you're getting now is caused by the fact that

print<int>(2);

Is interpreted as a typecast expression to convert 2 into a print<int>, which isn't what you want (and also isn't legal).

Hope this helps!

倾城月光淡如水﹏ 2025-01-10 11:15:02

对于那些无状态包装类,最好使用静态成员函数:

template<typename T, typename OutStream = std::ostream>
struct printer
{
    static OutStream & print()(T const &toPrint, OutStream &outStream = std::cout)
    {
        outStream << toPrint;
        return outStream;
    }
};

然后您可以使用 printer::print(x); 调用它们,并且通常可以提供类型推导辅助函数模板:

template <typename T> std::ostream & print(T const & x)
{
    return printer<T, std::ostream>::print(x);
}

现在您可以只说 print(x);

For those stateless wrapper classes, it might be better to use static member functions:

template<typename T, typename OutStream = std::ostream>
struct printer
{
    static OutStream & print()(T const &toPrint, OutStream &outStream = std::cout)
    {
        outStream << toPrint;
        return outStream;
    }
};

Then you can invoke them with printer<Foo>::print(x);, and you can typically supply a type-deducing helper function template:

template <typename T> std::ostream & print(T const & x)
{
    return printer<T, std::ostream>::print(x);
}

Now you can just say print(x);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文