模板化的operator()重载C++
有人已经问过这个问题,但该线程最终以原始问题没有得到回答。
假设你有这个:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
成员模板是一个从属名称,因为它的语义取决于
f_type
的类型。这意味着您应该将“template”放在其名称之前(以消除“小于”标记的使用歧义),类似于将typename
放在依赖限定名称之前:作为解决方法,您可以可以使用辅助类型:
现在,您可以按如下方式定义您的
operator()
:这对于模板化构造函数来说非常方便,因为您无法对其执行
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 puttypename
before dependent qualified names:As a workaround, you may use a helper type:
Now, you could define your
operator()
as follows: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.在像你这样的情况下,我会使用 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.
你能澄清一下吗? (伪代码或其他东西)
Can you clarify? (pseudocode, or something)