By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

Initializing std::basic_string<> with literals

P: n/a
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Dylan Nicholson wrote:
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"?

Jul 22 '05 #2

P: n/a


Dylan Nicholson schrieb:
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

P: n/a
Dylan Nicholson <wi******@hotmail.com> wrote:
[...]
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.
Dylan


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #4

P: n/a

"Dylan Nicholson" <wi******@hotmail.com> wrote in message
news:7d**************************@posting.google.c om...
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #5

P: n/a


Dylan Nicholson schrieb:
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #6

P: n/a
-----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 Nicholson wrote:
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-----

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #7

P: n/a
Jeff Schwab <je******@comcast.net> wrote in message news:<ku********************@comcast.com>...
Dylan Nicholson wrote:
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"?


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
Jul 22 '05 #8

P: n/a
Dylan Nicholson wrote:
Jeff Schwab <je******@comcast.net> wrote in message news:<ku********************@comcast.com>...
Dylan Nicholson wrote:
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"?

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.

Jul 22 '05 #9

P: n/a
Hi,

Dylan Nicholson wrote:
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.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #10

P: n/a


Evan Carew schrieb:

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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #11

P: n/a
-----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 Mang wrote:
Evan Carew schrieb:

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

-----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-----

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #12

P: n/a
Maciej Sobczak <no*****@no.spam.com> wrote in message news:<c0**********@atlantis.news.tpi.pl>...
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #13

P: n/a
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.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #14

P: n/a
Evan Carew <te*******@pobox.com> wrote in message
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

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.