Initializing std::basic_string<> with literals

D

Dylan Nicholson

Been playing around with this all day, and haven't found a solution I
like yet.

Assuming some initial function:

void foo(std::string& src)
{
src += "some fixed string";
src += bar();
src += "some other fixed string";
src += bar2();
src += "final fixed string";
}

What is the best way to turn this into a templated version that can
work on both std::basic_string<char> AND std::basic_string<wchar_t>?

Basically, I'd like something that looks (something) like:

template <class T>
void foo(std::basic_string<T>& src)
{
src += CVT("some fixed string");
src += bar();
src += CVT("some other fixed string");
src += bar2();
src += CVT("final fixed string";
}

Where CVT is a macro that expands to either the plain string literal
as is, or prepended with 'L' for the wchar_t version. Of course
because macro expansion is done prior to template evaluation, this
isn't directly possible.
I've played around with various options, but most of them produce
absurdly inefficient assembly-code (with various compilers), or just
look plain ugly and don't really make doing the same sort of thing in
the future any great deal easier.

Anyway, any ideas people may have on alternative ways of achieving
this effect are much appreciated!

Dylan
 
J

Jeff Schwab

Dylan said:
Been playing around with this all day, and haven't found a solution I
like yet.

Assuming some initial function:

void foo(std::string& src)
{
src += "some fixed string";
src += bar();
src += "some other fixed string";
src += bar2();
src += "final fixed string";
}

What is the best way to turn this into a templated version that can
work on both std::basic_string<char> AND std::basic_string<wchar_t>?

Basically, I'd like something that looks (something) like:

template <class T>
void foo(std::basic_string<T>& src)
{
src += CVT("some fixed string");
src += bar();
src += CVT("some other fixed string");
src += bar2();
src += CVT("final fixed string";
}

Where CVT is a macro that expands to either the plain string literal
as is, or prepended with 'L' for the wchar_t version. Of course
because macro expansion is done prior to template evaluation, this
isn't directly possible.
I've played around with various options, but most of them produce
absurdly inefficient assembly-code (with various compilers), or just
look plain ugly and don't really make doing the same sort of thing in
the future any great deal easier.

Anyway, any ideas people may have on alternative ways of achieving
this effect are much appreciated!

Would it be reasonable to specialize the template for each of "char" and
"wchar_t"?
 
T

Thomas Mang

Dylan said:
Been playing around with this all day, and haven't found a solution I
like yet.

Assuming some initial function:

void foo(std::string& src)
{
src += "some fixed string";
src += bar();
src += "some other fixed string";
src += bar2();
src += "final fixed string";
}

What is the best way to turn this into a templated version that can
work on both std::basic_string<char> AND std::basic_string<wchar_t>?

#include <string>

char const* bar();
wchar_t const* bar2();

template <typename charT, class traitsT, class allocatorT>
struct string_writer;

template <class traitsT, class allocatorT>
struct string_writer<char, traitsT, allocatorT>
{
typedef std::basic_string<char, traitsT, allocatorT> string_type;

static void write_string(string_type& String)
{
String += "some fixed string";
String += bar();
String += "some other fixed string";
String += "final fixed string";
}
};


template <class traitsT, class allocatorT>
struct string_writer<wchar_t, traitsT, allocatorT>
{
typedef std::basic_string<wchar_t, traitsT, allocatorT> string_type;

static void write_string(string_type& String)
{
String += L"some fixed string";
String += bar2();
String += L"some other fixed string";
String += L"final fixed string";
}
};


template <typename charT, class traitsT, class allocatorT>
void foo(std::basic_string<charT, traitsT, allocatorT> & String)
{
string_writer<charT, traitsT, allocatorT>::write_string(String);
}


int main()
{
std::basic_string<char> String1;
foo(String1);

std::basic_string<wchar_t> String2;
foo(String2);
}



Don't know if this is what you would call the 'best' solution, but it is
a working one.


regards,

Thomas
 
H

Hendrik Schober

Dylan Nicholson said:
[...]
template <class T>
void foo(std::basic_string<T>& src)
{
src += CVT("some fixed string");
src += bar();
src += CVT("some other fixed string");
src += bar2();
src += CVT("final fixed string";
}
[...]
Anyway, any ideas people may have on alternative ways of achieving
this effect are much appreciated!

Move your literals into a traits class
that's templatized on 'T'. Then you are
free to decide how you implement those.

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
J

John Harrison

Dylan Nicholson said:
Been playing around with this all day, and haven't found a solution I
like yet.

Assuming some initial function:

void foo(std::string& src)
{
src += "some fixed string";
src += bar();
src += "some other fixed string";
src += bar2();
src += "final fixed string";
}

What is the best way to turn this into a templated version that can
work on both std::basic_string<char> AND std::basic_string<wchar_t>?

Basically, I'd like something that looks (something) like:

template <class T>
void foo(std::basic_string<T>& src)
{
src += CVT("some fixed string");
src += bar();
src += CVT("some other fixed string");
src += bar2();
src += CVT("final fixed string";
}

Where CVT is a macro that expands to either the plain string literal
as is, or prepended with 'L' for the wchar_t version. Of course
because macro expansion is done prior to template evaluation, this
isn't directly possible.
I've played around with various options, but most of them produce
absurdly inefficient assembly-code (with various compilers), or just
look plain ugly and don't really make doing the same sort of thing in
the future any great deal easier.

Anyway, any ideas people may have on alternative ways of achieving
this effect are much appreciated!

Perhaps this (untested)

template <class C >
std::basic_string<C> CVT(const C* lit)
{
return lit;
}

john
 
T

Thomas Mang

Dylan said:
Been playing around with this all day, and haven't found a solution I
like yet.

Assuming some initial function:

void foo(std::string& src)
{
src += "some fixed string";
src += bar();
src += "some other fixed string";
src += bar2();
src += "final fixed string";
}

What is the best way to turn this into a templated version that can
work on both std::basic_string<char> AND std::basic_string<wchar_t>?

Another possible solution (maybe clearer than my previous one, because
less code duplication):


#include <cstring>
#include <string>
#include <algorithm>


char const* bar();
char const* bar2();


template <typename charT, class traitsT, class allocatorT>
struct string_converter;


template <class traitsT, class allocatorT>
struct string_converter<char, traitsT, allocatorT>
{
static std::basic_string<char, traitsT, allocatorT> convert(char const*
String)
{
return std::basic_string<char, traitsT, allocatorT>(String);
}
};


template <class traitsT, class allocatorT>
struct string_converter<wchar_t, traitsT, allocatorT>
{
static std::basic_string<wchar_t, traitsT, allocatorT> convert(char
const* String)
{
std::basic_string<wchar_t, traitsT, allocatorT> ReturnValue;

std::size_t const StringLength(std::strlen(String));
ReturnValue.reserve(StringLength);
std::copy(String, String + StringLength,
std::back_inserter(ReturnValue));

return ReturnValue;
}
};

template <typename charT, class traitsT, class allocatorT>
void foo(std::basic_string<charT, traitsT, allocatorT>& src)
{
typedef string_converter<charT, traitsT, allocatorT> sc;

src += sc::convert("some fixed string");
src += sc::convert(bar());
src += sc::convert("some other fixed string");
src += sc::convert(bar2());
src += sc::convert("final fixed string");
}

int main()
{
std::basic_string<char> String1;
foo(String1);

std::basic_string<wchar_t> String2;
foo(String2);
}


hope this helps.


regards,

Thomas
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dylan,

Unfortunately, C++'s string class doesn't support the operations of
PERL's or JAVA's string class such as mystring += "Something new to
add". In order to get this functionality in C++ you need to use one of
the string stream calsses as an intermediary. An example would be:

#include <strstream>

......

ostrstream mybuf;
mybuf << "Something new to add" << endl;
mybuf << "some other fixed string" << endl;

string returned_string = mybuf.str();


Evan Carew

Dylan said:
Been playing around with this all day, and haven't found a solution I
like yet.

Assuming some initial function:

void foo(std::string& src)
{
src += "some fixed string";
src += bar();
src += "some other fixed string";
src += bar2();
src += "final fixed string";
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFALPOtoo/Prlj9GScRAuPoAJ9Lr9B4UTpV39hXbbwUkQXBvSi95gCfQgDJ
hmEFyZxsELgMUaqvmABsoRI=
=3iZs
-----END PGP SIGNATURE-----
 
D

Dylan Nicholson

Jeff Schwab said:
Would it be reasonable to specialize the template for each of "char" and
"wchar_t"?

Er, how would you avoid writing exactly the same code twice then?

Actually I found a solution using a combination of macros and
overloaded functions (didn't even use templates in the end!); the only
drawback I found was that when used with single char literals it
wouldn't allow me to use them as a constant expression (even though
they clearly were). Of course on most platforms a char literal
promotes to a wchar_t literal correctly by default, but not on AS/400,
where char is EBCDIC and wchar_t is Unicode.

Dylan
 
J

Jeff Schwab

Dylan said:
Er, how would you avoid writing exactly the same code twice then?

If it's the same code, why are you writing it twice? It's different
(though similar) code.
Actually I found a solution using a combination of macros and
overloaded functions (didn't even use templates in the end!);

Mazel tov.
the only
drawback I found was that when used with single char literals it
wouldn't allow me to use them as a constant expression (even though
they clearly were). Of course on most platforms a char literal
promotes to a wchar_t literal correctly by default, but not on AS/400,
where char is EBCDIC and wchar_t is Unicode.

Hm. I wonder whether you still would have that problem if you weren't
circumventing the type system.
 
M

Maciej Sobczak

Hi,

Dylan said:
Been playing around with this all day, and haven't found a solution I
like yet.

I'm not sure whether this is OK (my two compilers I have access to right
at the moment cannot eat this stuff), but let's try:

template <class charT>
const charT *
typedLiteral(const char *str, const wchar_t *wstr);

template <>
const char *
typedLiteral<char>(const char *str, const wchar_t *wstr)
{
return str;
}

template <>
const wchar_t *
typedLiteral<wchar_t>(const char *str, const wchar_t *wstr)
{
return wstr;
}

#define CVT(type, str) typedLiteral<type>(str, L##str)

template <class T>
void foo(std::basic_string<T> & src)
{
src += CVT(T, "hello");
src += CVT(T, " world!");
}

The CVT macro should be used only with literals, duplicating them and
adding L in front of one of them. Later, the typedLiteral template
selects the one that is really needed.
 
T

Thomas Mang

Evan said:
Unfortunately, C++'s string class doesn't support the operations of
PERL's or JAVA's string class such as mystring += "Something new to
add".

Huhh?
Where did you get this from?

std::basic_string provides a whole bunch of overloaded operators, including
overloads of operator += (as member function) and other operators (such as
+ and comparison) as namespace-scope functions.

See 21.3.5.1 and 21.3.7.


regards,

Thomas
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tom,

Seems you are right. I don't know where I got that idea. Perhaps the
earlier versions of string were invariants.

Evan

Thomas said:
Evan Carew schrieb:




Huhh?
Where did you get this from?

std::basic_string provides a whole bunch of overloaded operators, including
overloads of operator += (as member function) and other operators (such as
+ and comparison) as namespace-scope functions.

See 21.3.5.1 and 21.3.7.


regards,

Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFALpv+oo/Prlj9GScRAmFKAJ9eky/P7WpupAcDIlZ9AvOn62JtawCeNeF+
N91Kjer/LTgYopGw4V/q0x8=
=t7Ps
-----END PGP SIGNATURE-----
 
D

Dylan Nicholson

Maciej Sobczak said:
Hi,


The CVT macro should be used only with literals, duplicating them and
adding L in front of one of them. Later, the typedLiteral template
selects the one that is really needed.

That's roughly what I did in the end, but as I need to support VC 6
which can't select template specializations with all the same argument
signatures, I passed in a dummy parameter, and didn't even bother
making them templates.
I'm just hoping that now it doesn't turn out that I actually need to
do it at runtime (eg using wcstombs) in order to control how the
conversion is done.

Dylan
 
S

Steve Haigh

Seems you are right. I don't know where I got that idea. Perhaps the
earlier versions of string were invariants.
I don't think so. In Java strings are immutable, perhaps this is the
source of the confusion?

BTW, it makes it a lot easier for others to follow up to posts if you
don't top-post.
 
B

Brian McKeever

Evan Carew said:
Unfortunately, C++'s string class doesn't support the operations of
PERL's or JAVA's string class such as mystring += "Something new to
add". In order to get this functionality in C++ you need to use one of
the string stream calsses as an intermediary. An example would be:

As Thomas Mang pointed out, std::string does support operators + and
+=. I think you've reversed Java and C++ in your head. Java supports
the 'string1 += "foo";' syntax, but the compiler translates this into
the equivalent of your suggested code:
ostrstream mybuf;
mybuf << "Something new to add" << endl;
mybuf << "some other fixed string" << endl;

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top