[xsd-users] Feature request: Explicit constructors
Boris Kolpackov
boris at codesynthesis.com
Mon Nov 22 08:50:11 EST 2010
Hi Benjamin,
Benjamin Schindler <bschindler at inf.ethz.ch> writes:
> Ray proposed to change my constructor to const char*:
>
> "I would resolve the ambiguity by adding a constructor to MyClass:
> MyClass(char const*, double)"
He actually didn't propose to change it, he proposed to add another
overloaded version (MyClass(char const*, double)) which resolves
the ambiguity. In this constructor you can decide what should be
initialized with a C-string in MyClass.
> I agree that this is convenient. But how many people expect that this
> could throw?
>
> void someFunc(FrameKind kind);
> someFunc("SomeInvalidEnumerator"); ?
>
> I mean seriously, just a function call itself should not throw. And if
> you only want to allow string constants, I'd suggest removing the
> std::string constructor.
>
> I thought that it is common knowledge that implicit conversions should
> be discouraged as much as possible - but if you disagree then I'll have
> to live with that.
I think implicit conversion has its uses. In this case, the object,
conceptually, "is a" string and initializing it implicitly with
another kind of a string seems natural. Yes, it can throw. If you
don't want it to throw, then use the C++ enum interface:
void someFunc(FrameKind::value kind);
someFunc(FrameKind::SomeInvalidEnumerator); // Compile-time error.
Boris
More information about the xsd-users
mailing list