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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:42:11+00:00 2026-05-11T21:42:11+00:00

When I compile the following code using g++ class A {}; void foo(A&) {}

  • 0

When I compile the following code using g++

class A {};

void foo(A&) {}

int main()
{
  foo(A());
  return 0;
}

I get the following error messages:

> g++ test.cpp -o test     
test.cpp: In function ‘int main()’:
test.cpp:10: error: invalid initialization of non-const reference of type ‘A&’ from a temporary of type ‘A’
test.cpp:6: error: in passing argument 1 of ‘void foo(A&)’

After some reflection, these errors make plenty of sense to me. A() is just a temporary value, not an assignable location on the stack, so it wouldn’t seem to have an address. If it doesn’t have an address, then I can’t hold a reference to it. Okay, fine.

But wait! If I add the following conversion operator to the class A

class A
{
public:
  operator A&() { return *this; }
};

then all is well! My question is whether this even remotely safe. What exactly does this point to when A() is constructed as a temporary value?

I am given some confidence by the fact that

void foo(const A&) {}

can accept temporary values according to g++ and all other compilers I’ve used. The const keyword can always be cast away, so it would surprise me if there were any actual semantic differences between a const A& parameter and an A& parameter. So I guess that’s another way of asking my question: why is a const reference to a temporary value considered safe by the compiler whereas a non-const reference is not?

  • 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-11T21:42:11+00:00Added an answer on May 11, 2026 at 9:42 pm

    It isn’t that an address can’t be taken (the compiler could always order it shoved on the stack, which it does with ref-to-const), it’s a question of programmers intent. With an interface that takes a A&, it is saying “I will modify what is in this parameter so you can read after the function call”. If you pass it a temporary, then the thing it “modified” doesn’t exist after the function. This is (probably) a programming error, so it is disallowed. For instance, consider:

    void plus_one(int & x) { ++x; }
    
    int main() {
       int x = 2;
       float f = 10.0;
    
       plus_one(x); plus_one(f);
    
       cout << x << endl << f << endl;
    }
    

    This doesn’t compile, but if temporaries could bind to a ref-to-non-const, it would compile but have surprising results. In plus_one(f), f would be implicitly converted to an temporary int, plus_one would take the temp and increment it, leaving the underlying float f untouched. When plus_one returned, it would have had no effect. This is almost certainly not what the programmer intended.


    The rule does occasionally mess up. A common example (described here), is trying to open a file, print something, and close it. You’d want to be able to do:

    ofstream("bar.t") << "flah";
    

    But you can’t because operator<< takes a ref-to-non-const. Your options are break it into two lines, or call a method returning a ref-to-non-const:

    ofstream("bar.t").flush() << "flah";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't use asynchronous replication in both directions without creating… May 11, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer You haven't provided enough information - this depends on the… May 11, 2026 at 11:46 pm
  • Editorial Team
    Editorial Team added an answer You could join on another table, for example SELECT *… May 11, 2026 at 11:46 pm

Related Questions

This is similar to (but different from) this question . Here is some simple
I get the following errors when trying to compile the below code using g++.
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
I'm trying to learn how to create a C/C++ library in a linux environment

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.