[xsd-users] Feature request: Explicit constructors
Benjamin Schindler
bschindler at inf.ethz.ch
Fri Nov 19 10:58:48 EST 2010
On 11/19/2010 03:53 PM, Boris Kolpackov wrote:
> Hi Benjamin,
>
> Benjamin Schindler <bschindler at inf.ethz.ch> writes:
>
>> There are a few problems here:
>> - the implicit conversion from const char* to std::string is required by
>> the standard (I thought) and natural because you're basically converting
>> a string to a string
>
> Correct.
>
>
>> - Converting a string to an enum by implicit conversion is a very
>> suspicious operation at best (Is the value defined? If not, exception?
>> Error?). If you really want it, make it explicit by calling
>> FrameKind("myString") - I'm not trying to disable that. This also makes
>> you aware that this constructor might throw etc. I consider this a lot
>> more clean tells the developer clearly what's happening.
>
> You can argue it either way. In your schema you define the enum by
> deriving from xsd:string. So this type also "is a" string, just
> like std::string (and, in fact, derives from). In C++/Tree string-
> based enums have dual interface: you can work with them as with
> strings or as C++ enums. Most users find it convenient to be able
> to implicitly initialize a enum with a string literal.
>
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.
More information about the xsd-users
mailing list