【问题标题】:How do I call a templatized operator()()?如何调用模板化的 operator()()?
【发布时间】:2012-06-21 17:36:54
【问题描述】:

我有一个看起来像这样的结构:

struct foo_t
{
    template <std::size_t x, std::size_t y>
    std::size_t operator()() const
    { return /*something dealing with x and y*/; }
};

定义似乎编译得很好,但是我该如何称呼它呢?我似乎无法通过编译器:

foo_t foo;
foo<3, 3>(); // ERROR: Compiler seems to think I'm asking for "foo < 3 ..."

【问题讨论】:

  • 您可能不想这样做...使用operator 重载通常有助于语法,但如果您需要提供模板参数,它会使您的代码更丑陋。
  • 可能是一个错字,但现在你的语法有点错误。它需要是ret_type operator()()(或者第二对括号可以声明一些参数类型)。在这种情况下,我想知道你为什么要这样做……

标签: c++ templates compiler-errors operators


【解决方案1】:

很丑,但是..,

foo_t foo;
foo.operator()<3, 3>();

Online demo.

【讨论】:

  • +1。每次我看到这样的 sn-p 时,我都会为编译器实现者感到难过 :-) 让我受益匪浅的不是模板,而是字符串 ()&lt;3, 3&gt;() 有一个有效的上下文。
  • @Cameron 我通常说编译模板有一些魔力:P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多