模板化的operator()重载C++

发布于 2024-08-11 06:47:22 字数 1114 浏览 3 评论 0原文

有人已经问过这个问题,但该线程最终以原始问题没有得到回答。

假设你有这个:

template<size_t i, class f_type>
void call_with_i(f_type f);

functor_type 是:

a) 具有以下签名的方法的结构:

template<size_t i> operator()() const;

或者,b)看起来像这样的函数:

template<size_t i> foo();

我希望“call_with_i<42>(foo)”等同于“foo<42>()”,但我可以'不找出正确的语法来实现这一点。我会对一个解决方案感到满意,它只执行 (a) 但 (a)+(b) 会很棒。我已经尝试过这些语法:

f< i >(); // doesn't work
f()< i >; // doesn't work
f.operator< i >(); // doesn't work
f.operator()< i >; // doesn't work
f.operator()< i >(); // works on msvc, but doesn't work on gcc. 

如何使用显式模板参数调用operator()? 有没有办法以相同的语法调用模板化自由函数的方式来调用它?

ps如果你想知道我用它做什么,那是因为我我正在编写一个函数repeat_to,其中repeat_to<10>(f) 调用f(0),然后调用f(1) ... f(10)。我使用它来按索引并行迭代多个 boost::fusion 向量。是的,我可以使用迭代器,或者我可以只使用命名成员函数,但我仍然想知道答案。

编辑说明:我删除了一些东西,因为将模板化的自由函数作为参数传递没有任何意义。

someone already asked this question, but the thread ended up with the original question not getting answered.

suppose you have this:

template<size_t i, class f_type>
void call_with_i(f_type f);

functor_type is either:

a) a struct with a method that has the following signature:

template<size_t i> operator()() const;


or, b) a function that looks like this:

template<size_t i> foo();

I want "call_with_i<42>(foo)" to be equivalent to "foo<42>()", but I can't figure out the right syntax to make that happen. I'd be satified with a solution that does just (a) but (a)+(b) would be great. I've already tried these syntaxes:

f< i >(); // doesn't work
f()< i >; // doesn't work
f.operator< i >(); // doesn't work
f.operator()< i >; // doesn't work
f.operator()< i >(); // works on msvc, but doesn't work on gcc. 

How do you invoke operator() with explicit template arguments? Is there a way to invoke it in a way that the same syntax would also call a templated free function?

p.s. If you're wondering what i'm using this for, its because I'm writing a function repeat_to where repeat_to<10>(f) invokes f(0) then f(1) ... f(10). I'm using this to iterate through multiple boost::fusion vectors in parallel by index. yeah, i could use iterators, or i could just use a named member function, but i still want to know the answer.

edit note: i striked out stuff because passing a templated free function as an arg doesn't make any sense.

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

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

发布评论

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

评论(3

我是有多爱你 2024-08-18 06:47:22

成员模板是一个从属名称,因为它的语义取决于 f_type 的类型。这意味着您应该将“template”放在其名称之前(以消除“小于”标记的使用歧义),类似于将 typename 放在依赖限定名称之前:

template<size_t i, class f_type>
void call_with_i(f_type f) {
  f.template operator()<i>();
  // f.template foo<i>();
}

作为解决方法,您可以可以使用辅助类型:

template<size_t N> struct size_t_ { }; // or boost::mpl::int_

template<size_t i, class f_type>
void call_with_i(f_type f) {
  f(size_t_<i>());
}

现在,您可以按如下方式定义您的 operator()

template<size_t i> void operator()(size_t_<i>) const {
  // i was deduced automatically by the function argument. 
}

这对于模板化构造函数来说非常方便,因为您无法对其执行 f_type()()< /代码>或其他东西。在这种情况下,它们必须是可推论的。

The member template is a dependent name, because its semantics depend on the type of f_type. That means you should put "template" before its name (to disambiguate the use of the "less-than" token), similar to how you should put typename before dependent qualified names:

template<size_t i, class f_type>
void call_with_i(f_type f) {
  f.template operator()<i>();
  // f.template foo<i>();
}

As a workaround, you may use a helper type:

template<size_t N> struct size_t_ { }; // or boost::mpl::int_

template<size_t i, class f_type>
void call_with_i(f_type f) {
  f(size_t_<i>());
}

Now, you could define your operator() as follows:

template<size_t i> void operator()(size_t_<i>) const {
  // i was deduced automatically by the function argument. 
}

This comes handy for templated constructors, for which you cannot do f_type()<i>() or something. They will have to be deducible in that case.

ゞ花落谁相伴 2024-08-18 06:47:22

在像你这样的情况下,我会使用 boost::function 作为函子类型。然后,您可以传递函数对象和函数指针,同时保留相同的接口。

In a case like yours I would use boost::function as functor type. You can then pass both function objects and function pointers while retaining the same interface.

像你 2024-08-18 06:47:22
#include <iostream>

template<size_t i, class f_type> void call_with_i(f_type f);

struct A {

    template < size_t i >
    void operator()() const {
        /* no link err in demo */
    }

    template < size_t i >
    void foo() {
        /* no link err in demo */
    }
};

int main(int argc, char * const argv[]) {
    A f;

    enum { Constant = 42 };

    f.operator()<Constant>();
    f.foo<Constant>();

    return 0;
}

有没有办法以相同的语法调用模板化自由函数的方式调用它?

你能澄清一下吗? (伪代码或其他东西)

#include <iostream>

template<size_t i, class f_type> void call_with_i(f_type f);

struct A {

    template < size_t i >
    void operator()() const {
        /* no link err in demo */
    }

    template < size_t i >
    void foo() {
        /* no link err in demo */
    }
};

int main(int argc, char * const argv[]) {
    A f;

    enum { Constant = 42 };

    f.operator()<Constant>();
    f.foo<Constant>();

    return 0;
}

Is there a way to invoke it in a way that the same syntax would also call a templated free function?

Can you clarify? (pseudocode, or something)

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