Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3320734
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:58:43+00:00 2026-05-17T22:58:43+00:00

What is a recommended way to overload the output stream operator? The following can

  • 0

What is a recommended way to overload the output stream operator? The following can not be done. It is expected that compilation will fail if the operator << is not defined for a type T.

template < class T >
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v) 
{
    os << "[";
    for (std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
    {
        os << " " << *ii;
    }
    os << " ]";
    return os;
}

EDIT: It does compile, the problem was unrelated and was in the namespace. Thanks for assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:58:43+00:00Added an answer on May 17, 2026 at 10:58 pm

    Did you actually try this code? It works fine on gcc with a small tweak std::vector<T>::const_iterator, needs to be declared as typename std::vector<T>::const_iterator

    You may be better off with using std::copy and std::ostream_iterator.

    EDIT: types, dependent types and typename
    Can’t fit it all in the comments, so here goes (btw. this is my understanding and I could be off by a country mile – if so please correct me!)…

    I think this is best explained with a simple example..

    Let’s assume you have a function foo

    template <typename T>
    void foo()
    {
      T::bob * instofbob; // this is a dependent name (i.e. bob depends on T)
    };
    

    Looks okay, and typically you may do this

    class SimpleClass
    {
      typedef int bob;
    };
    

    And call

    foo<SimpleClass>(); // now we know that foo::instofbob is "int"
    

    Again, seems self explanatory, however some nuser comes along and does this

    class IdiotClass
    {
      static int bob;
    };
    

    Now

    foo<IdiotClass>(); // oops, 
    

    What you have now is an expression (multiplication) as IdiotClass::bob resolves to a non-type!

    To the human, it’s obvious that this is stupid, but the compiler has no way of differentiating between types vs. non-types, and by default in C++ (and I think this is where compilers differ), all qualified dependent names (i.e. T::bob) will be treated as non-type. To explicitly tell the compiler that the dependent name is a real type, you must specify the typename keyword –

    template <typename T>
    void foo()
    {
      typedef typename T::bob *instofbob; // now compiler is happy, it knows to interpret "bob" as a type (and will complain otherwise!)
    };
    

    This applies even if it is a typedef. i.e.

    template <typename T>
    void foo()
    {
      typedef typename T::bob local_bob;
    };
    

    Is that any clearer?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats the recommended way for storing authentication information in iOS that could be easily
What is the recommended way of launching a modeless window but enforcing that there
Possible Duplicate: What’s the right way to overload operator== for a class hierarchy? I
What is the recommended way of handling the following type of situations: Supposing I
I know that for a modal view controller the recommended way of dismissing it
What is the recommended way to provide a WCF call that has a Generic
Is there a recommended way of implementing a series of activities such that the
What's the recommended way to use Maven for a project that might grow large
What is the recommended way to ensure that the mainform is fully maximized and
What is the recommended way to add Spring Security to a web application that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.