模板操作符()重载C ++ [英] templated operator() overload C++

查看:137
本文介绍了模板操作符()重载C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设您有这个问题:

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

functor_type是:



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

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


或,b)一个类似这样的函数:

  template< size_t i> foo(); 



call_with_i< 42>(foo)等价于foo< 42>(),但是我不能找出正确的语法来使其发生。我会被一个解决方案,只是(a)<打击>但(a)+(b)将是伟大的。我已经尝试过这些语法:

  f< (i)。 //不工作
f()< i。 //不工作
f.operator< (i)。 // does not work
f.operator()< i。 // does not work
f.operator()< (i)。 // works on msvc,but does not work on gcc。

如何使用显式模板参数调用operator 是否有一种方法调用它的方式,同样的语法也将调用模板的自由函数?



p.s。如果你想知道我使用它,因为我写一个函数repeat_to其中repeat_to< 10>(f)调用f(0)然后f(1)... f(10)。我使用这个迭代通过多个boost ::融合矢量并行的索引。是的,我可以使用迭代器,或者我可以只使用一个成员函数,但我仍然想知道答案。



编辑注释:一个模板的自由函数作为一个arg没有任何意义。

解决方案

成员模板是一个依赖名称,取决于 f_type 的类型。这意味着你应该在它的名称之前加上template(以消除less-than标记的使用),类似于你应该如何在依赖的限定名称之前放置 typename

 模板< 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_ {}; //或boost :: mpl :: int_ 

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

现在,您可以定义 operator / code>如下:

  template< size_t i> void operator()(size_t_< i>)const {
// i由函数参数自动推导出来。
}

这对于模板化构造函数来说非常方便,你不能做 f_type()< i>()或某事。他们将 在这种情况下可以推断。


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.

解决方案

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.

这篇关于模板操作符()重载C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆