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 6060311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:48:40+00:00 2026-05-23T08:48:40+00:00

I am wondering. I hit a problem and here is a small repoduce. Essentially

  • 0

I am wondering. I hit a problem and here is a small repoduce. Essentially i want to forward everything. The problem is, using the first << will cause an error with o<<1 (or o<<SomeUserStruct(). If i include the second i get errors about it being ambiguous. Is there a way i can write this code so it will use T& when it can otherwise uses T?

#include <iostream>
struct FowardIt{
    template<typename T> FowardIt& operator<<(T&t) { std::cout<<t; return *this; }
    //template<typename T> FowardIt& operator<<(T t) { std::cout<<t; return *this; }
};

struct SomeUserStruct{};

int main() {
    FowardIt o;
    o << "Hello";
    int i=1;
    o << i;
    o << 1;
    o << SomeUserStruct();
}
  • 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-23T08:48:41+00:00Added an answer on May 23, 2026 at 8:48 am
    template<typename T> FowardIt& operator<<(const T&t)
                                            //^^^^^ put const here
    

    Make the parameter const reference, as shown above. Because temporaries cannot be bound to non-const reference. You don’t need to define another function. Just make the parameter const, the problem will be solved.

    It would also be better if you make the function template const as well by putting const to the rightmost side of the function as:

    template<typename T> 
    const FowardIt& operator<<(const T&t) const
    ^^^^^                      ^^^^^      ^^^^^
      |                          |          |
      |                          |          put const here as well
      |                          put const here
      |
      You've to make the return-type also const
      since it can't return non-const reference anymore
    

    If you do so, then you can call this function on const objects as well:

    void f(const FowardIt &o)//note: inside the function, o is an const object!
    {
        o << 1;
        o << SomeUserStruct();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if anyone has gotten the infamous database is locked error from Trac and
Wondering if anybody out there has any success in using the JDEdwards XMLInterop functionality.
I'm faced with setting up a system which will allow users to: Hit a
all, I have some problem installing my apk file in code. Wondering if anyone
I am puzzled at a cygwin encoding problem. I am using its Mintty 0.8.3.
I've been hard at this problem for a bit and am wondering if anybody
I'm having a problem with performance with the entity framework. Here's the scenario. I
Wondering if there is any Text to Speech software available as a plug in
Wondering if I need to do something in my swf to be able to
Wondering how to open many new windows with Javascript. I have found plenty of

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.