473,387 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Initializing std::basic_string<> with literals

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


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

"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


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


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
-----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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is...
0
by: Rémi Peyronnet | last post by:
Hello, I have some link problems while using basic_string with another type (unsigned int in my case) This is a sample source : " #include <string>
4
by: Jurko Gospodnetić | last post by:
Hi all. I was wondering. Can the standard basic_string<> c_str() member function throw any exceptions? Is it perhaps implementation dependent? I tried checking the standard and as far as I...
1
by: mufasa | last post by:
have a type given by: typedef std::basic_string<wchar_t> mystringtype; at one instacne of my program, i have a vairable called 'abc' of type mystringtype. When i try to access the .c_str() or...
5
by: Hendrik Schober | last post by:
Hi, I had originally suspected this to be a bug in the std lib, that's why I started a thred in the STL newsgroup. However, it seems as if it is a compiler bug, so I'm transfering it to here....
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
0
by: pillbug | last post by:
I am trying to convince the Greta regular expression library to use the following char_traits: struct ignore_case_traits : std::char_traits<char> { static bool eq (const char& x, const char& y)...
1
by: sharmadeep1980 | last post by:
Hi All, I am facing a very unique problem while compling my project in "Release" build. The project is building in DEBUG mode but giving linking error on Release build. Here is the error:...
7
by: huili80 | last post by:
Should complex<T>::real() and imag() return a value or a refernce? What does the standard say about this? I just realized that MSVC2008's implementation returns a value, but in GCC reference is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.