[xsd-users] Feature request: Explicit constructors
Benjamin Schindler
bschindler at inf.ethz.ch
Fri Nov 19 08:09:49 EST 2010
Hi
On 11/19/2010 01:59 PM, Ray Lischner wrote:
> Benjamin Schindler <bschindler at inf.ethz.ch> writes:
>> In C++, implicit type conversions are usually not recommended due to the
>> difficult predictability of performance. Using explicit as much as
>> possible is recommended, see i.e.
>> http://www.doc.ic.ac.uk/lab/cplus/c++.rules/chap13.html
>
>> I know they are useful - but they are only useful until you get hit by a
>> type conversion you didn't expect causing weird bugs, at which point you
>> usually stop using them for 99% percent of the code. For a generic
>> library such as xsd doubly so
>
> The MyClass constructors that are already defined rely on implicit construction of std::string from char const*.
> My recommendation is to add a constructor to bypass that implicit construction and to be explicit about the construction of MyClass from char const*.
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
- 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.
- The bypass doesn't really work, see the example below:
void someFunction(const char* arg);
std::string myString = "someValue";
someFunction(myString); // does not compile, but I require that
I know, there is c_str(), but now we're really working around a problem.
Cheers
Benjamin
>
> Ray Lischner,
> Senior Member of Technical Staff
> 133 National Business Pkwy, Ste 150 t. 443.539.3448
> Annapolis Junction, MD 20701 c. 410.854.5170
> rlischner at proteuseng.com f. 443.539.3370
>
> This electronic message and any files transmitted with it contain information
> which may be privileged and/or proprietary. The information is intended for use
> solely by the intended recipient(s). If you are not the intended recipient, be
> aware that any disclosure, copying, distribution or use of this information is
> prohibited. If you have received this electronic message in error, please advise
> the sender by reply email or by telephone (443.539.3400) and delete the message.
More information about the xsd-users
mailing list