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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:44:23+00:00 2026-05-16T22:44:23+00:00

I would like to return a noncopyable object of type Foo from a function.

  • 0

I would like to return a noncopyable object of type Foo from a function. This is basically a helper object which the caller will use to perform a set of actions, with a destructor to perform some cleanup after the actions are complete.

Before the advent of rvalue references, I would have returned a shared_ptr<Foo> or something similar. With rvalue references, another option would be to make the constructor and copy constructor private, and have the only public constructor be a move constructor. Foo would look something like this:

class Foo : boost::noncopyable
{
private:
    Foo( /* whatever the real ctor needs */ );

public:
    Foo( Foo && src );

    // ... interesting stuff ...
};

Foo a( SomethingThatReturnsFoo() ); // allowed
Foo b;      // error, no public default constructor
Foo c( a ); // error, noncopyable
Foo d = a;  // error, noncopyable

My question is whether it would be bad form to do this, or whether it looks reasonable. I can’t think of any reason why this would cause issues or be difficult to read, but I’m still somewhat of a newbie when it comes to rvalue references, so there might be considerations I’m not thinking of.

  • 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-16T22:44:24+00:00Added an answer on May 16, 2026 at 10:44 pm

    This isn’t bad form at all- consider objects like mutexes or scoped objects like unique_ptr. Unique_ptr is movable but not copyable and it’s part of the STL.

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

Sidebar

Related Questions

I have a function from which I would like to return a value as
From a function, I would like to return a modified version of an object
I would like to return a DTO from my data layer which would also
I would like to return an ExpandoObject from a WebMethod, like this: [WebMethod] public
I would like to return two values from a function in two separate variables.
I have a function within a class which I would like to return the
I would like to return a std::function whose type is dependent on the type
So basically I would like my app to read info from a database, this
I would like to return the id of a newly created object for my
I have a method where I would like to return an object instance of

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.