This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC] std::string(0);


Hi,

I would like to suggest a small improvement to basic_string. As the subject already says, it's about std::string(0);

Obviously, it's illegal to pass a const char* to the ctor of basic_string that is zero. The current implementation results in a SEGV. I would like to catch *some* of the cases at compile-time instead of having a crashing program. :)

My suggestion is to add a declaration for a private ctor which takes a single argument of type 'int'. The ctor shouldn't be defined and it cannot be used anyway. No bloat should happen (except a very small amount of compilation time) and it's just a single line to add - thus the cost is very small. Also, AFAICS, the ctor won't interfer with any other ctor from basic_string.

The benefit of this ctor is, that it catches all cases where a basic_string is constructed from a literal zero. IMHO this case is common enough to be worth the change. An example which recently wasted my colleagues and my time is, when you pass parameters to functions:

void f( const std::string& s ) { ... }

f( 0 ); // Oops...

The real world example was (of course) a bit more complex, there were several parameters and the function originally needed an 'int'. Only after changing the parameter from 'int' to 'std::string', we forgot to fix some calls to the function, thus the temporary std::string was constructed from 0. It took quite some time to debug the code until we finally found out what the problem was. It's IMHO a pitty that such a obvious errors wasn't caught at compile-time.

So: As the costs are rather small, I'd like to see such a protection in basic_string. How about it?

Regards, Daniel

--
Daniel Frey

aixigo AG - financial solutions & technology
Schloß-Rahe-Straße 15, 52072 Aachen, Germany
fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99
eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]