简体   繁体   中英

boost to_upper char pointer in one expression

Is it possible to do something like:

const char* str = "AaBbCc";
string 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM