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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:39:18+00:00 2026-05-22T18:39:18+00:00

Example: Foo make_foo(int a1, int a2){ Foo f(a1,a2); return f; } Having seen such

  • 0

Example:

Foo make_foo(int a1, int a2){
  Foo f(a1,a2);
  return f;
}

Having seen such functions several times, is it just a matter of coding style / preference or is there more to it than meets the eye? Specifically this answer got me thinking with the make_unique implementation and the claim it is exception safe – is that related to the splitting of creation and return? Or am I reading too much into this? Why not simply write

Foo make_foo(int a1, int a2){
  return Foo(a1,a2);
}
  • 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-22T18:39:18+00:00Added an answer on May 22, 2026 at 6:39 pm

    Note that the answer to which you refer actually has something different:

    std::unique_ptr<T> ret (new T(std::forward<Args>(args)...));
    

    In this line of code, explicit dynamic allocation is performed. Best practices dictate that whenever you perform explicit dynamic allocation, you should immediately assign the result to a named smart pointer. For more details, consult the Boost shared_ptr best practices documentation or Herb Sutter’s GotW article, “Exception-Safe Function Calls.”

    It isn’t always dangerous to have a new expression as a subexpression of a larger expression, but it’s a rule that is easy to forget, so it’s best to always follow the best practices guideline and assign a new dynamically allocated object to a named smart pointer.


    That said, there is at least one advantage to the pattern of creating a named object and then returning it: it can be a bit easier to “watch” the object in the debugger when stepping through code quickly.

    One possible disadvantage is that it is potentially more difficult for a compiler to perform Return Value Optimization (RVO) with a named object. Named Return Value Optimization (NRVO) is not always as straightforward as RVO with an unnamed temporary. I’d hazard to guess that modern compilers wouldn’t have a problem either way, but I am not an expert on C++ compiler optimizations.

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

Sidebar

Related Questions

For example: javac Foo.java Note: Foo.java uses unchecked or unsafe operations. Note: Recompile with
As an example: public class Foo { private Foo() {} } public class Bar
For example: Base class header file has: enum FOO { FOO_A, FOO_B, FOO_C, FOO_USERSTART
For example, suppose I have a class: class Foo { public: std::string& Name() {
First, two examples: // This works int foo = 43; long lFoo = foo;
Is there any difference to the following code: class Foo { inline int SomeFunc()
Visual Studio keeps trying to indent the code inside namespaces. For example: namespace Foo
In C++. I can declare most things as const, for example: Variables: const int
Example: You have a shortcut s to SomeProgram in the current directory. In cmd.exe
Example I have Person , SpecialPerson , and User . Person and SpecialPerson are

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.