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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:27:30+00:00 2026-05-25T16:27:30+00:00

Can someone explain the output of the following code? #include <iostream> template <class T>

  • 0

Can someone explain the output of the following code?

#include <iostream>

template <class T>
void assign(T& t1, T& t2){
    std::cout << "First method"<< std::endl;
    t1 = t2;
}

template <class T>
void assign(T& t1, const T& t2) {
    std::cout << "Second method"<< std::endl;
    t1 = t2;
}

class A
{
public:
    A(int a) : _a(a) {};

private:
    friend A operator+(const A& l, const A& r);

    int _a;
};

A operator+(const A& l, const A& r) 
{
    return A(l._a + r._a);
}

int main ()
{
    A a = 1;
    const A b = 2;

    assign(a, a);
    assign(a, b);
    assign(a, a + b);
}

The output is

First method

Second method

Second method

I don’t see why. Shouldn’t the last call to assign activate the first version, since (a+b) doesn’t return a const A object?

  • 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-25T16:27:31+00:00Added an answer on May 25, 2026 at 4:27 pm

    An expression doesn’t only have a value and a type, but it also has a value category. This category can be

    • An lvalue: These expressions generally refer to declared objects, references, functions or dereference results of pointers.
    • An xvalue: These are the result of generating an unnamed rvalue reference. Rvalue references are created by T&& instead of T&. They are a C++11 concept, and you can ignore them here. Mentioned only for sake of completeness.
    • An prvalue: These are the results of casts to non-reference types (like A(10)) or computing/specifying a value, like 42 or 2 + 3.

    An lvalue reference requires an lvalue expression for initialization. That is, the following is invalid:

    A &x = A(10);
    

    The reason behind this is that only lvalue expressions refer to things that are suitable and intended for staying alive a longer time than only for the duration of the initialization. Like, a declared object is alive until exiting its block (if it was a local non-static variable) or until the end of the program (if it was declared outside functions and classes). The rvalue expression A(10) refers to an object that dies already when the initialization is finished. And if you said the following, it would not make any sense of all, because pure values like 10 don’t have an address at all, but references require some sort of identity to which they bind, which in practice is implemented by taking the address of their target internally in compilers

    int &x = 10; // makes totally no sense
    

    But for const references, C++ has a backdoor. When initialized with a prvalue, a const lvalue reference will automatically lengthen the lifetime of the object, if the expression refers to an object. If the expression has a non-object value, C++ creates a temporary object with a value of that expression, and lengthens the lifetime of that temporary, binding the reference to that temporary:

    // lifetime of the temporary object is lengthened
    A const& x = A(10); 
    
    // lifetime of the automatically created temporary object is lengthened
    int const& x = 10; 
    

    What happens in your case?

    Now the compiler in your case, because you supply a temporary object, will choose the version that has a A const& parameter type rather than a A& parameter type.

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

Sidebar

Related Questions

Can someone explain the output of the following code char* a[] = {ABC123, DEF456,
Can someone help explain the following: If I type: a=`ls -l` Then the output
Can someone please explain why this program outputs 0x00000004? class AndAssignment { static void
Can someone explain the following two outputs? Code 1: console.log(itemsAry); //loadNextItem(); function loadNextItem(){ var
Can someone please explain this to me? I have the following code: <form action=<?php
Can someone explain why the following code... #!/opt/local/bin/perl use strict; use warnings; my $string;
Can someone explain why this script throws an exception? $byteArray = @(1,2,3) write-Output (
Can someone explain why how the result for the following unpack is computed? aaa.unpack('h2H2')
Can someone explain the following behavior? Specifically, why does the function return a different
Can someone explain the following strange behavior? I run the following program on a

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.