字符串文字转换为basic_string< unsigned char> [英] String literal to basic_string<unsigned char>

查看:197
本文介绍了字符串文字转换为basic_string< unsigned char>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谈到国际化和Unicode,我是个白痴的美国程序员.这是交易.

When it comes to internationalization & Unicode, I'm an idiot American programmer. Here's the deal.

#include <string>
using namespace std;

typedef basic_string<unsigned char> ustring;

int main()
{
    static const ustring my_str = "Hello, UTF-8!"; // <== error here
    return 0;
}

这发出了意外的投诉:

cannot convert from 'const char [14]' to 'std::basic_string<_Elem>'

也许我今天喝了错误的咖啡.我该如何解决?我可以保留基本结构吗?

Maybe I've had the wrong portion of coffee today. How do I fix this? Can I keep the basic structure:

ustring something = {insert magic incantation here};

?

推荐答案

狭窄的字符串文字被定义为const char,并且没有无符号的字符串文字[1],因此您必须进行强制转换:

Narrow string literals are defined to be const char and there aren't unsigned string literals[1], so you'll have to cast:

ustring s = reinterpret_cast<const unsigned char*>("Hello, UTF-8");

当然,您可以将较长的内容放入内联函数中:

Of course you can put that long thing into an inline function:

inline const unsigned char *uc_str(const char *s){
  return reinterpret_cast<const unsigned char*>(s);
}

ustring s = uc_str("Hello, UTF-8");

或者您可以只使用basic_string<char>并在处理UTF-8时有99.9%的时间摆脱它.

Or you can just use basic_string<char> and get away with it 99.9% of the time you're dealing with UTF-8.

[1]除非char是无符号的,但是无论它是否是实现定义的,等等,等等.

[1] Unless char is unsigned, but whether it is or not is implementation-defined, blah, blah.

这篇关于字符串文字转换为basic_string&lt; unsigned char&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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