重载运算符|对于固定大小的数组? [英] Overload operator| for fixed-size arrays?

查看:52
本文介绍了重载运算符|对于固定大小的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下事项:

template <typename T> struct template_adapter_t {};

template <int N> struct foo_adapter_t {
  template <typename T> static foo_t<T, N> adapt(T const&);
};
template <int N> template_adapter_t< foo_adapter_t<N> > foo();

template <typename Range, typename Adapter>
auto operator|(
  Range const& range,
  template_adapter_t<Adapter>(*)())
-> decltype(Adapter::adapt(range))
{ return Adapter::adapt(range); }

(所以,这里发生了什么......我有一个虚拟"类,template_adapter_t,它仅用作特化限制器,一个具体的帮助类foo_adapter_t,一个自由函数 foo,它返回一种 template_adapter_t 类型,以及一个 operator| 的重载,它应该在 LHS 上接受任何东西,以及一个指向不带参数并在 RHS 上返回某种类型的 template_adapter_t 的函数的指针.)

(So, what is going on here... I have a 'dummy' class, template_adapter_t, that serves only as a specialization limiter, a concrete helper class foo_adapter_t, a free function foo which returns a type of template_adapter_t, and an overload of operator| that is supposed to take anything on the LHS, and a pointer to a function that takes no parameters and returns some type of template_adapter_t on the RHS.)

当我尝试使用它时:

int const values[] = { ... };
values | foo<2>;

...我收到关于无效操作数的错误.

...I get an error about invalid operands.

但是,这是有效的:

std::vector<int>{} | foo<2>;

有效:

template <typename Range, int N>
auto operator|(
  Range const& range,
  foo_adapter_t<N> const& adapter)
-> decltype(adapter.adapt(range))
{ return adapter.adapt(range); }

values | foo_adapter_t<4>{};

...所以问题似乎不在于 LHS 上的匹配.在这两种情况下,RHS 也是模板化的,但由于某种原因,采用结构的方法有效,但采用函数的方法无效.

...so the problem doesn't seem to be with matching on the LHS. In both cases, the RHS is also templated, but for some reason, the one taking a struct works, but the one taking a function does not.

我错过了什么,替代调用有效但第一个无效?我怎样才能使第一个表单工作?

What am I missing that the alternate invocations work but the first one doesn't? How can I make the first form work?

实例

推荐答案

这是因为您不能为 2 个指针重载运算符.至少一个操作数应该是使用定义的类(参见这里).

This is because you can't overload operators for 2 pointers. At least one of the operand should be used-defined class (See here).

在这一行:

values | foo<2>;

values 是指向 const int 的指针,foo<2> 是函数指针,因此不会考虑重载运算符.

values is a pointer to const int, foo<2> is a function pointer, thus no overloaded operator will be considered.

这篇关于重载运算符|对于固定大小的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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