Thread: How do I overload ostream::operator<<

  1. #1
    Registered User
    Join Date
    Feb 2011

    How do I overload ostream::operator<<

    Hi, everyone. This code compiles and runs, producing the expected output:

    #include <iostream>
    template<typename T, std::size_t n>
    std::ostream& operator<<(std::ostream& os, const T (&arr)[n])
        std::size_t i;
        for (i = 0; i < n; ++i)
            if (i > 0)
                os << ' ';
            os << arr[i];
        return os;
    int main()
        int n[10] = { 1,2,3,4,5,6,7,8,9,10 };
        std::string s[2] = { "Hello, world", "Goodbye, Earth" };
        std::cout << n << '\n';
        std::cout << s << '\n';
    My concern is that the line os << ' '; must be given a single character. If I give it a zero-terminated char array (such as " ") then the compiler doesn't know whether to recurse into my user-defined function, or use the regular ostream:: operator<<().

    How do I say "use the one that you had before this function existed"? Or even better, "don't use this function if you already have one", or "don't use this one for char arrays".

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Don't do this; write a named function (template) for output instead. Prefer to reserve the overloading of operator<< for ostream for class types (or class templates, if applicable) that have a canonical output format. This way, you follow the principle of least surprise.
    Last edited by laserlight; 11-21-2018 at 06:34 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    You could do something like this:
    #include <iostream>
    #include <string>
    template<typename T, std::size_t N>
    struct arrayout_obj {
        const T (&arr)[N];
        const char *sep;
        arrayout_obj(const T (&arr)[N], const char *sep) : arr(arr), sep(sep) {}
    template<typename T, std::size_t N>
    auto arrayout(const T (&arr)[N], const char *sep = ", ") {
        return arrayout_obj<T,N>(arr, sep);
    template<typename T, std::size_t N>
    std::ostream& operator<<(std::ostream& os, const arrayout_obj<T,N>& ap) {
        for (std::size_t i = 0; i < N; ++i) {
            if (i > 0) os << ap.sep;
            os << ap.arr[i];
        return os;
    int main() {
        int n[] = { 1,2,3,4,5,6,7,8,9,10 };
        std::string s[] = { "Mercury", "Venus", "Earth", "Mars" };
        std::cout << arrayout(n) << '\n';
        std::cout << arrayout(s, "\n");
    Last edited by john.c; 11-21-2018 at 12:57 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ostream operator overloading error
    By lancehumiston in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2012, 06:36 AM
  2. c++ operator overload
    By cable in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2011, 11:56 AM
  3. Friend operator << with ostream
    By Freem in forum C++ Programming
    Replies: 8
    Last Post: 07-18-2011, 09:27 AM
  4. Overload operator
    By verbity in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2007, 11:00 AM
  5. ostream operator<< () overload problems
    By jaybny in forum C++ Programming
    Replies: 2
    Last Post: 06-24-2006, 11:00 PM

Tags for this Thread