Hi list, In 'classic' C++ style I have signalled optional parameters in a function with pointers that might be null (I wouldn't like to create overloads for all possible combination of existing and non-existing parameters) e.g. // p1 is mandatory, p2, p3 and p4 are optional void f(const Param1& p1, const Param2* p2, const Param3* p3, const Param4* p4); And call it Param1p1; Param2 p2; Param4 p4; f(p1, &p2, 0, &p4); I thought I could use boost::optional like this void f(const Param1& p1, const boost::optional<Param2> & p2, const boost::optional<Param3> p3, const boost::optional<Param4> & p4); And call it Param1p1; Param2 p2; Param4 p4; f(p1, boost::optional<Param2>(p2), boost::optional<Param3>(), boost::optional<Param4>(p4) ); but I haven't seen this usage in the examples in the documentation of Boost.Optional. Is this usage correct? Are there any hidden drawbacks with this approach? TIA
2009/5/6
Hi list,
In 'classic' C++ style I have signalled optional parameters in a function with pointers that might be null (I wouldn't like to create overloads for all possible combination of existing and non-existing parameters) e.g.
// p1 is mandatory, p2, p3 and p4 are optional void f(const Param1& p1, const Param2* p2, const Param3* p3, const Param4* p4);
And call it Param1p1; Param2 p2; Param4 p4; f(p1, &p2, 0, &p4);
I thought I could use boost::optional like this
void f(const Param1& p1, const boost::optional<Param2> & p2, const boost::optional<Param3> p3, const boost::optional<Param4> & p4);
And call it Param1p1; Param2 p2; Param4 p4; f(p1, boost::optional<Param2>(p2), boost::optional<Param3>(), boost::optional<Param4>(p4) );
It's less efficient, because all arguments will be copied. It's also more typing, longer compilation times and more code generated. Roman Perepelitsa.
2009/5/6 Roman Perepelitsa
It's less efficient, because all arguments will be copied. It's also more typing, longer compilation times and more code generated.
There is one more caveat. This approach does not work with polymorphic types (you'll get slicing). I don't think there are any advantages in passing boost::optional<T> compared to const T*. Roman Perepelitsa.
Roman Perepelitsa
2009/5/6 Roman Perepelitsa
It's less efficient, because all arguments will be copied. It's also more
typing, longer compilation times and more code generated.
There is one more caveat. This approach does not work with polymorphic types
(you'll get slicing). I don't think there are any advantages in passing boost::optional<T> compared to const T*.Roman Perepelitsa.
Thanks for the answer!
Is there a way to avoid the copy (and thus the slicing)? Perhaps using
boost::optional
2009/5/6 dariomt
Roman Perepelitsa
writes: 2009/5/6 Roman Perepelitsa
It's less efficient, because all arguments will be copied. It's also more typing, longer compilation times and more code generated.
There is one more caveat. This approach does not work with polymorphic
types (you'll get slicing). I don't think there are any advantages in passing boost::optional<T> compared to const T*.Roman Perepelitsa.
Thanks for the answer!
Is there a way to avoid the copy (and thus the slicing)? Perhaps using boost::optional
or boost::optional< boost::reference_wrapper<T> > ?
You can use boost::optional
Igor R wrote:
f(p1, boost::optional<Param2>(p2), boost::optional<Param3>(), boost::optional<Param4>(p4) );
The params would be implicitly converted, so you can call like this: f(p1, p2, boost::optional<Param3>(), p4);
boost::none will be helpful as well: f(p1, p2, boost::none, p4);
On Wed, May 6, 2009 at 4:44 AM, Gevorg Voskanyan
Igor R wrote:
f(p1, boost::optional<Param2>(p2), boost::optional<Param3>(), boost::optional<Param4>(p4) );
The params would be implicitly converted, so you can call like this: f(p1, p2, boost::optional<Param3>(), p4);
boost::none will be helpful as well: f(p1, p2, boost::none, p4);
Beside the headers, Google doesn't yield documentation on boost::none. Could someone please explain it's intended use? I see none_t is a typedef to a member of none_helper, a struct with no members, and none is a const none_t, but I'd expect such a pointer to not be convertible to anything, so I'm not following on passing boost::none to a boost::optional as above, nor can't figure out the use for none. Can someone please provide some details / background info on none? Thanks, --DD
AMDG Dominique Devienne wrote:
On Wed, May 6, 2009 at 4:44 AM, Gevorg Voskanyan
wrote: boost::none will be helpful as well: f(p1, p2, boost::none, p4);
Beside the headers, Google doesn't yield documentation on boost::none.
Could someone please explain it's intended use? I see none_t is a typedef to a member of none_helper, a struct with no members, and none is a const none_t, but I'd expect such a pointer to not be convertible to anything, so I'm not following on passing boost::none to a boost::optional as above, nor can't figure out the use for none. Can someone please provide some details / background info on none? Thanks
http://tinyurl.com/cuehvn In Christ, Steven Watanabe
Dominique Devienne wrote:
boost::none will be helpful as well: f(p1, p2, boost::none, p4);
Beside the headers, Google doesn't yield documentation on boost::none.
Could someone please explain it's intended use? I see none_t is a typedef to a member of none_helper, a struct with no members, and none is a const none_t, but I'd expect such a pointer to not be convertible to anything, so I'm not following on passing boost::none to a boost::optional as above, nor can't figure out the use for none. Can someone please provide some details / background info on none? Thanks, --DD
This is described in the documentation of boost.optional, here: http://www.boost.org/doc/libs/1_39_0/libs/optional/doc/html/boost_optional/d... Best Regards, Gevorg
participants (7)
-
dariomt
-
dariomt@gmail.com
-
Dominique Devienne
-
Gevorg Voskanyan
-
Igor R
-
Roman Perepelitsa
-
Steven Watanabe