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

  • Home
  • SEARCH
  • 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 3952276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:49:53+00:00 2026-05-20T01:49:53+00:00

I have some problems overloading operators with template members and using make_pair: class MyArchive

  • 0

I have some problems overloading operators with template members and using make_pair:

class MyArchive
{
    public:
    template <class C> MyArchive & operator<< (C & c)
    {

        return (*this);
    }
};

class A
{

};

int main()
{

    MyArchive oa;
    A a;
    oa << a; //it works
    oa << std::make_pair(std::string("lalala"),a); //it doesn't work
    return 0;
}

I get this interesting error:

/home/carles/tmp/provaserialization/main.cpp: In function ‘int main()’:
/home/carles/tmp/provaserialization/main.cpp:30: error: no match for ‘operator<<’ in ‘oa << std::make_pair(_T1, _T2) [with _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = A]((a, A()))’
/home/carles/tmp/provaserialization/main.cpp:11: note: candidates are: MyArchive& MyArchive::operator<<(C&) [with C = std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, A>]

Any ideas about why it doesn’t find operator<< in the second case?

  • 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-20T01:49:54+00:00Added an answer on May 20, 2026 at 1:49 am

    The parameter of operator<< should be const :

    template <class C> MyArchive & operator<< (const C & c)
    

    Because std::make_pair returns a temporary object which cannot be bound to non-const parameter. But a temporary object can be bound to a const parameter, since then the life of the temporary extends, till the end of the called function.


    A simple demonstration:

    template<typename T>
    void f(T & c) { cout << " non-const parameter" << endl; }
    
    template<typename T>
    void f(const T & a) { cout << "const parameter" << endl; }
    
    int main() 
    {
         f(make_pair(10,20.0)); //this calls second function!
    }
    

    Output:

    const parameter

    See the output yourself here: http://www.ideone.com/16DpT

    EDIT:

    Of course, the above output explains only that temporary is bound to the function with const-parameter. It doesn’t demonstrate life-extension. The following code demonstrates life-extension:

    struct A 
    {
      A() { cout << "A is constructed" << endl; }
      ~A() { cout << "A is destructed" << endl; }
    };
    
    template<typename T>
    void f(T & c) { cout << " non-const parameter" << endl; }
    
    template<typename T>
    void f(const T & a) { cout << "const parameter" << endl; }
    
    int main() 
    {
         f(A()); //passing temporary object!
    }
    

    Output:

    A is constructed
    const parameter
    A is destructed

    The fact that A is destructed after the function prints const parameter demonstrates that A’s life is extended till the end of the called function!

    Code at ideone : http://www.ideone.com/2ixA6

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

Sidebar

Related Questions

I have a class using operator overloading, but there are some warnings. // base.h
I am creating a KeyValuePair class, and am running into some trouble when overloading
I'm working through some home work and having problems with how to form my
I'm confused about friend operator overloading. It has no problem if I write the
I have written some code in Java that will decode a SHOUTcast stream and
I'm still new to C++ so I daily run into new problems. Today came
I am in a classic design dilemma. I am writing a C# data structure
I'm following a tutorial on php, and am having difficulty getting this to work.
Is there any PHP PDF library that can replace placeholder variables in an existing

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.