How to parse comma separated values with boost::program_options?

泪湿孤枕 提交于 2019-12-01 21:23:15

You will have to make the operator>> discoverable.

Because it needs to operate on a left-hand-side of std::istream& it cannot be declared in the "owning" class; But being a free function, you will need to employ namespace lookups in order for the code to find the streaming operator.

Now note that the streaming operator is being called from somewhere inside namespace boost::detail (from the Boost Lexicalcast library).

It uses argument dependent lookup to select the overload. ADL implies that the namespaces associated with the argument types indicate which namespaces should be searched for candidate operator>> overloads.¹

This means that the lookup will search namespace std (due to the std::istream argument) and also lli (due to the second argument type). Note that if any of the argument types itself uses a template argument type, the namespace(s) defining that are also included in the lookup.

As you noted, you can get around this

  • by not using a namespace.
  • Or, you can define the operator>> overload inside the lli namespace: Live On Coliru
  • Alternatively, declare it in-class (as a friend static function, which will be treated as if were declared in the containing namespace): Live On Coliru

¹ it works for non-operator free functions just the same, though

For some reason it can be compiled (no errors like above) if i just remove namespace lli and it works as expected ..

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