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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:04:01+00:00 2026-06-03T06:04:01+00:00

class A { public: A() { cout << A() << endl; } A(const A&)

  • 0
class A
{
public:
    A()
    {
        cout << "A()" << endl;
    }

    A(const A&)
    {
        cout << "A(const A&)" << endl;
    }

    A(A&&)
    {
        cout << "A(A&&)" << endl;
    }

    A& operator=(const A&)
    {
        cout << "A(const A&)" << endl;
    }

    A& operator=(A&&)
    {
        cout << "A(const A&&)" << endl;
    }

    ~A()
    {
        cout << "~A()" << endl;
    }
};

A&& f_1()
{
    A a;
    return static_cast<A&&>(a);
}

A f_2()
{
    A a;
    return static_cast<A&&>(a);
}

int main()
{
    cout << "f_1:" << endl;
    f_1();
    cout << "f_2:" << endl;
    f_2();
}

the output is:

f_1:
A()
~A()
f_2:
A()
A(A&&)
~A()
~A()

The sample code obviously indicates that f_1() is more efficient than f_2().

So, my question is:

Should we always declare a function as some_return_type&& f(…); instead of some_return_type f(…); ?

If the answer to my question is true, then another question follows:

There have been many many functions declared as some_return_type f(…); in the C++ world, should we change them to the modern form?

  • 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-06-03T06:04:03+00:00Added an answer on June 3, 2026 at 6:04 am

    Is it a good practice that declaring a function as BigStruct&& foo(…);

    Horrible practice. As with lvalue-references what you are doing is returning a reference to a local object. By the time the caller receives the reference the referred object is already gone.

    There have been many many functions declared as some_return_type f(…); in the C++ world, should we change them to the modern form?

    The modern form is the form in which they were already built (considering only return types). The standard was changed to make the common form more efficient, not to have everyone rewrite all their code.

    Now there are non-modern versions out there, like void f( type& t ) instead of type f() when f creates the object. Those we want to change to the modern form type f() as that provides a simpler interface to reason about and provide for simpler user code:

    Users don’t need to think whether the object is modified or created:

    void read_input( std::vector<int>& );
    

    Does that append or replace the vector?

    And make caller code simpler:

    auto read_input();
    

    vs.

    std::vector<int> v; 
    read_input(v);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Sample { public: Sample(){} Sample(const Sample& obj){ cout<<C.C. with 1 argument called<<endl;} Sample(const
class A { public: A(const int n_); A(const A& that_); A& operator=(const A& that_);
class Widget { public: Widget() { cout<<~Widget()<<endl; } ~Widget() { cout<<~Widget()<<endl; } void* operator
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
class apple { public : operator orange () const { cout << operator; }
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
class foo { public: void say_type_name() { std::cout << typeid(this).name() << std::endl; } };
class SimpleCat { public: SimpleCat(); SimpleCat(SimpleCat&); ~SimpleCat(); }; SimpleCat::SimpleCat() { cout << Simple Cat
[a follow up to this question ] class A { public: A() {cout<<A Construction
#include <iostream> using namespace std; class A { public: A() { cout << Default

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.