This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Mingw-w64-public] toUpper()


On 06/30/2015 05:24 PM, Riot wrote:
     #include <algorithm>
     #include <string>

     std::string str = "Hello World";
     std::transform(str.begin(), str.end(), str.begin(), std::toupper);

Please note this code is subtly incorrect for two reasons.
There are two overloads of std::toupper:

1) int toupper(int) declared in <ctype.h> (and the equivalent
   std::toupper in <cctype>)
2) template <class T> charT std::toupper(T, const locale&)
   in <locale>

Without the right #include directive, the above may or may
not resolve to "the right" function (which depends on what
declarations the two headers bring into scope).

When it resolves to (2) it will fail to compile.

When it resolves to (1), it will do the wrong thing (have
undefined behavior) at runtime when char is a signed type
and the argument is negative (because (1) is only defined
for values between -1 and UCHAR_MAX).

But the question is about converting std::wstring to upper
case and the above uses a narrow string. For wstring, the
std::ctype<wchar_t>::toupper() function or its convenience
non-member template function can be used.

See also: http://www.cplusplus.com/reference/locale/toupper/

This is one possible way to do it. Another approach is along
these lines:

   std::locale loc (...);
   std::wstring wstr = L"...";
   const std::ctype<wchar_t> &ct =
       std::use_facet<std::ctype<wchar_t> >(loc);
   ct.toupper (&wstr[0], &wstr[0] + wstr.size());

Martin


This may also help in future: http://lmgtfy.com/?q=c%2B%2B+toupper

-Riot

On 30 June 2015 at 23:58,  <papa@arbolone.ca> wrote:
I would like to write a function to capitalize letters, say...
std::wstring toUpper(const std::wstring wstr){
for ( auto it = wstr.begin(); it != wstr.end(); ++it){
         global_wapstr.append(std::towupper(&it));

}
}

This doesnât work, but doesnât the standard already have something like
std::wstring::toUpper(...)?

Thanks in advance


---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com


------------------------------------------------------------------------------
Don't Limit Your Business. Reach for the Cloud.
GigeNET's Cloud Solutions provide you with the tools and support that
you need to offload your IT needs and focus on growing your business.
Configured For All Businesses. Start Your Cloud Today.
https://www.gigenetcloud.com/
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]