Can operators be overloaded for initializer_list literals? [duplicate]

南笙酒味 提交于 2019-12-04 03:13:22

As far as I understand 13.5/6,

An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration

this should not be possible. Braced-init-lists are not the same as std::initializer_lists, and they're not interchangeable. The following should work, though:

std::initializer_list<int>({1,2}) + std::initializer_list<int>({2,1})

Or this:

operator+({1,2}, {2,1})

Update: To clarify, the point is that there's no rule in the language grammar that allows a braced-init-list to appear in the context suggested by the OP. Braced-init-lists can only appear in contexts where some­thing is about to be initialized, if you will.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!