Discussion Locked

Constant string literals

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Tell us what you think about our tip on using constant string literals. Will this tip help you when coding templated functions?

3 replies

by spi

In my humble opinion, this is too complicated... and not readable.
What about the following code?

template< class C >
C* StrAOrW( char* as, wchar_t* ws );

template<>
inline char* StrAOrW( char* as, wchar_t* ws )
{ return as; }

template<>
inline unsigned char* StrAOrW( char* as, wchar_t* ws )
{ return (unsigned char*)as; }

template<>
inline wchar_t* StrAOrW( char* as, wchar_t* ws )
{ return ws; }

Isn't this more readable, and hence, more maintanable?
I believe that C++ programmers are too often blind to STL way of coding.

If we (C++ programmers), don't try to simplify our code to make it as readable as possible (? la Java), but still efficient, C++ may simply die!

?Keep It Simple Stupid? is my motto.

by spi

I already commented the last column about constant string literals.
Here again I think that the same goal can be reached in a really more
readable way. Last time I defined one (and only one) template function
StrAOrW( char* as, wchar_t* ws ) with 3 specializations.
The semantics of this function is simply "choose the good one according to C type".
In the following code, one struct and only one new StrAOrW function (which relies on the 3 previous ones) are enough:

struct AOrWString {
AOrWString( char* a, wchar* w )
: _as(a), _ws(w) {}
char* _as;
wchar* _ws;
};

template< class C >
inline C* StrAOrW( AOrWString& s )
{ return StrAOrW( s._as, s._ws ); }

#define AORWSTR( name, s ) AOrWString name( s, L ## s )Isn't it simpler? more easily understood ?
And what about maintanability ?

by john torjo

You're totally right.
This is much easier to maintain and understand.

To my shame, I did not think about it.

Thanks for your feedback.

Best,
John

This discussion is closed to new replies.