boost::any: explicit constructor?

Hi, I just had a couple of bugs caused by the fact that constructor of boost::any is not explicit. For example: void do_something(boost::any& v) { ... } class variable { ///// boost::any& value() {} }; variable v; do_something(v); The intention was to call do_somethin on 'v.value()' but non-explicit ctor is happy to accept 'variable', after which 'do_something' breaks. Anybody has an opinion? Is non-explicit ctor really necessary? - Volodya

Vladimir Prus wrote:
Hi, I just had a couple of bugs caused by the fact that constructor of boost::any is not explicit. For example:
void do_something(boost::any& v) { ... }
class variable { ///// boost::any& value() {} };
variable v; do_something(v);
The intention was to call do_somethin on 'v.value()' but non-explicit ctor is happy to accept 'variable', after which 'do_something' breaks.
This is a programming error, not an error in boost::any.
Anybody has an opinion? Is non-explicit ctor really necessary?
I don't think it should be explicit. After all, the idea is that one can pass any type to boost::any. Forcing an explicit constructor defeats the ease of use which boost::any represents among other things. If the programmer passes a variable of a type which he didn't mean to pass, that is his problem. I don't think you should force him to telegraph that he is creating a boost::any variable by forcing an explicit constructor. C++ has always existed based on the presumption that the programmer knows what he is doing. While I am not against syntactic sugar which make it easier to use C++ constructs, I am against making a constructor explicit just to keep programmers from making errors.
participants (2)
-
Edward Diener
-
Vladimir Prus