使std :: string成为union的成员??? [英] Making a std::string a member of a union ???
问题描述
除了从头开始创建自己的字符串之外,还有这样做吗?
union AnyType {
std :: string String;
double number;
};
Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
推荐答案
" Peter Olcott" < No **** @ SeeScreen.com在新闻中写道:ZtPoh.8718
"Peter Olcott" <No****@SeeScreen.comwrote in news:ZtPoh.8718
rv1.8337
@ newsfe21.lga:
rv1.8337
@newsfe21.lga:
除了从头开始创建自己的字符串之外还有这样做吗?
union AnyType {
std :: string String;
double Number;
};
Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
即使使用自己的字符串也不能这样做:
第12.1.11节:工会成员不应是具有非平凡构造函数的类类型(或数组
)。
You can''t do this even with your own string:
Section 12.1.11: "A union member shall not be of a class type (or array
thereof) that has a non-trivial constructor.".
Peter Olcott schrieb :
Peter Olcott schrieb:
除了从头开始创建自己的字符串之外还有这样做吗?
union AnyType {
std :: string String;
double Number;
};
Is there anyway of doing this besides making my own string from scratch?
union AnyType {
std::string String;
double Number;
};
你不能。但是根据你想做什么,你可能会发现
boost :: variant或boost :: any非常有用:
http://www.boost.org/doc/html/variant.html
< a rel =nofollowhref =http://www.boost.org/doc/html/any.htmltarget =_ blank> http://www.boost.org/doc/html/any.html
-
Thomas
http://www.netmeister.org/news/learn2quote.html
You cannot. But depending on what you want to do, you might find
boost::variant or boost::any quite useful:
http://www.boost.org/doc/html/variant.html
http://www.boost.org/doc/html/any.html
--
Thomas
http://www.netmeister.org/news/learn2quote.html
这篇关于使std :: string成为union的成员???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!