开发者

boost to_upper char pointer in one expression

开发者 https://www.devze.com 2023-02-18 00:08 出处:网络
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\"

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

精彩评论

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