初始化std :: basic_string<>用文字 [英] Initializing std::basic_string<> with literals

查看:100
本文介绍了初始化std :: basic_string<>用文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整天都玩这个,但还没有找到我想要的解决方案。


假设有一些初始功能:


void foo(std :: string& src)

{

src + =" some fixed string" ;;

src + = bar();

src + ="其他一些固定字符串" ;;

src + = bar2();

src + =" final fixed string" ;;

}


将此转换为可以<的模板化版本的最佳方法是什么? br />
适用于std :: basic_string< char> AND std :: basic_string< wchar_t> ;?


基本上,我想要的东西看起来像(像):


template< ;类T>

void foo(std :: basic_string< T>& src)

{

src + = CVT("一些固定的字符串);

src + = bar();

src + = CVT(其他一些固定的字符串);

src + = bar2();

src + = CVT(最终固定字符串;

}


其中CVT是一个宏,可以按原样扩展为普通字符串文字

,或者在wchar_t版本中以''L'为前缀。当然

因为宏扩展是在模板评估之前完成,这个

是不可能直接实现的。

我玩过各种各样的选项,但大多数都生产

荒谬无效的汇编代码(带有各种编译器),或者只是

看起来很丑陋而且实际上并没有做同样的瘦g in

未来任何更轻松的事情。


无论如何,人们可能有任何关于实现其他方式的想法

这个效果非常感谢!


Dylan


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang。 C ++。主持。第一次海报:做到这一点! ]

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

推荐答案

Dylan Nicholson写道:
Dylan Nicholson wrote:
一整天都在玩这个,并没有找到解决方案我还是喜欢。

假设一些初始功能:

void foo(std :: string& src)
{
src + =" some fixed string" ;;
src + = bar();
src + =" some other fixed string" ;;
src + = bar2();
src + =最终固定字符串;
}

将其转换为可以在std :: basic_string< char>上工作的模板化版本的最佳方法是什么? AND std :: basic_string< wchar_t>?

基本上,我想要一些看起来像某样的东西:

模板< class T>
void foo(std :: basic_string< T>& src)
{
src + = CVT(" some fixed string");
src + = bar();
src + = CVT(其他一些固定字符串);
src + = bar2();
src + = CVT(最终固定字符串;
}
其中CVT是一个宏,它可以扩展为普通的字符串文字,或者以L为前缀,用于wchar_t版本。当然,因为宏扩展是在事先完成的对于模板评估,这不可能直接实现。
我已经玩过各种各样的选项,但是大多数都会产生非常低效的汇编代码(包含各种编译器),或者只是看起来很丑陋而且真的没有做同样的事情在未来任何更轻松的事情。

无论如何,任何非常感谢人们可能对其他方式实现这种效果的想法!
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!




专门化每个char模板的模板是否合理?和

" wchar_t"?



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





Dylan Nicholson schrieb:


Dylan Nicholson schrieb:
一整天都在玩这个游戏,但我还没有找到解决方案。

假设有一些初始功能:

void foo(std :: string& src)
{
src + =" some fixed string" ;;
src + = bar();
src + ="一些其他固定字符串" ;;
src + = bar2();
src + ="最终固定字符串" ;;
}

什么是将它变成模板化版本的最好方法,它可以在std :: basic_string< char>上工作AND std :: basic_string< wchar_t>?
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();


模板< typename charT,类traitsT,类allocatorT>

struct string_writer;


模板< 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" ;

}

};

模板< 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" ;

}

};

模板< 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);

}


不知道这是不是你称之为' '最好'的解决方案,但它是

a工作一个。

问候,


Thomas

[见 http://www.gotw.ca/resources /clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



#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! ]


Dylan Nicholson< wi ****** @ hotmail.com>写道:
Dylan Nicholson <wi******@hotmail.com> wrote:
[...]
模板< class T>
void foo(std :: basic_string< T>& src)
{
src + = CVT(一些固定字符串);
src + = bar();
src + = CVT(其他一些固定字符串);
src + = bar2();
src + = CVT(最终固定字符串;
}

无论如何,人们可能对其他方式有任何想法实现
这种效果非常受欢迎!


将你的文字转移到一个特质类

''''''''''''''''''''''''''''''''''''''然后你就可以自由决定如何实现这些。

Dylan
[...]
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 永远不会阅读

我是Schobi at suespammers dot org


有时编译器比人们更合理。

Scott Meyers


[见 http://www.gotw.ca/resources/clcm.htm info about

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



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


这篇关于初始化std :: basic_string&lt;&gt;用文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆