This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: [RFC] std::string(0);
Daniel Frey wrote:
Martin Sebor wrote:
A conforming implementation is not allowed to reject constructs
such as std::string (0) just because they might cause undefined
behavior at runtime. Consider this well-formed program:
#include <string>
int main () {
if (0) std::string (0);
}
Martin
OK, so this means we can't go the "easy" way. The remaining options are
(AFAICS):
a) Change the standard, make the above example illegal. Unlikely.
I would not be happy about such a change -- our implementation
defines useful behavior in this case :) (it allocates storage
for the string object without initializing it).
b) Ask the compiler folks to detected these cases and emit a warning.
Bonus points for suppressing the warning on dead code branches like
above. Sadly, I suppose it's ways more work than adding one or two lines
to the STL implementation. Any volunteers? ;)
I don't think this would work, either. The compiler may not be
able to tell whether a branch of code is dead or not. Consider
this:
#include <string>
int main (int argc, char**) {
if (argc > 1) std::string (0);
}
Martin