开发者

boost to_upper char pointer in one expression

Is it possible to do something like:

const char* str = "AaBbCc";
st开发者_JAVA技巧ring str_up = boost::to_upper_copy(str); // str_up will be "AABBCC"

Last line doesn't compile.

Of course I can as below, but it less "canonical":

const char* str = "AaBbCc";

string str_up = str;
boost::to_upper(str_up);


The declaration of to_upper_copy is:

template<typename SequenceT> 
SequenceT to_upper_copy(const SequenceT &, const std::locale & = std::locale());

From this it should be clear that SequenceT can't be a pointer to char, or even a char array, because there's no (good) way how the returned copy could have the same type.

You can explicitly force the type to be string:

string str_up = boost::to_upper_copy<string>(str); 

Here's explanation what SequenceT and RangeT mean in the documentation: String Representation. In short, neither can be a const char*, but RangeT arguments accept arrays (char [X]).


Why not post the right solution as an answer to the question?

 const char* str = "AaBbCc";
 std::string str_up = boost::to_upper_copy(std::string(str));

Credits to Konrad.


Sure!

inline std::string to_upper_copy(std::string const & src) {
   std::string res(src);
   boost::to_upper(res);
   return res;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜