Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Override "operator <<" for char and wchar_t combined
Forum Update on Monday, May 27th 2025

Override "operator <<" for char and wchar_t combined

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 2 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    twoflower
    wrote on last edited by
    #1

    This is a C++ question I guess. Do you know how to override "operator <<" to handle both char and wchar_t types without the use of wcout for wchar_t?

    I would guess it need to be done with templates but I'm struggling to understand how to use templates here to implement it/ code it.

    Example:

    const char * string1 = "I love Qt"
    const wchar_t * string2 = "I love Qt"

    I can cout << string1 but need to use wcout << string2

    How or can I somehow use/code a generic genout << that combines cout and wcout by using template I guess or what ever it takes?

    1 Reply Last reply
    0
    • U Offline
      U Offline
      utcenter
      wrote on last edited by
      #2

      With this operator both char[] and w_char[] will work with cout, assuming you pass null terminated strings:

      @template<typename T>
      ostream& operator<<(ostream& out, T text[])
      {
      while (*text) {
      out << (char)*text;
      ++text;
      }
      return out;
      }@

      1 Reply Last reply
      0
      • T Offline
        T Offline
        twoflower
        wrote on last edited by
        #3

        That cast to (char) will only strip away the top bits of a Unicode character. If you look at the basic Unicode table "here":http://www.tamasoft.co.jp/en/general-info/unicode.html, you can imagine that will get you.

        If your compiler supports type information, you can try to detect the type of the character and output accordingly, but that still doesn't fix anything for the first parameter of the << operator if you will be doing the override described above.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on last edited by
          #4

          Or even easier, just switch to wcout - with this operator overload BOTH cout and wcout work with BOTH char and wchar_t, just cast to wchar_t to eliminate cutting off values, promoting char to wchar_t should be trouble-free.

          @template<typename T>
          ostream& operator<<(ostream& out, T* text)
          {
          while (*text) {
          out << (wchar_t)*text;
          ++text;
          }
          return out;
          }

          std::wcout << string1 << endl << string2 << endl;@

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved