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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:40:44+00:00 2026-06-17T16:40:44+00:00

Problem: I have a non-copyable object with two constructors. I need to create an

  • 0

Problem: I have a non-copyable object with two constructors. I need to create an object with one of the constructors and then use it within some common code:-

With a copyable object it would look like this, and be easy:

Object a;

if (condition) 
   a = Object(p1);
else
   a = Object(p2,p3,p4);

a.doSomething();

But, the object is non-copyable, so I’ve had to do this:

boost::scoped_ptr<Object> a;

if (condition) 
   a = new Object(p1);
else
   a = new Object(p2,p3,p4);

a->doSomething();

This feels too complex. Is there a better solutiuon?

  • 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-17T16:40:44+00:00Added an answer on June 17, 2026 at 4:40 pm

    Here’s a very terrible hack, assuming Object is default-constructible:

    Object a;
    a.~Object();
    
    if (condition) { ::new (&a) Object(p1); }
    else           { ::new (&a) Object(p2, p3, p4); }
    

    Don’t use this.

    Another option is using a union, but you’ll need to invoke the destructor manually in that setup as well.


    A cleaner solution could be achieved with Boost.Optional (using in-place factories). (Thanks to @K-Ballo for digging up the details!)

    #include <boost/optional.hpp>
    #include <boost/utility/in_place_factory.hpp>
    
    struct Object
    {
        explicit Object(int) {}
        explicit Object(int, float, std::string) {}
    
        Object(Object const &)             = delete;
        Object(Object &&)                  = delete;
        Object & operator=(Object const &) = delete;
        Object & operator=(Object &&)      = delete;
    };
    
    boost::optional<Object> a;
    
    if (condition) { a = boost::in_place(0); }
    else           { a = boost::in_place(0, 1.0f, "two" ); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem to test my non activity-class which need the context of
Problem: I have two array where one produce a category and the second produce
We have two mailing lists that we need to combine, and then make sure
I have a menu where some items are non-dropdowns and others are. The problem
I have a non-static C# class with some instance methods, which I need to
I am doing homework but I have problem with non-RestFul routes. My spec is:
I have a problem displaying non- ASCII characters in Matplotlib, these characters are rendered
Hugs seems to have a problem with several non-enbraced !! in a partial application.
I have the following code: The actual problem is the non-quoted code. I want
Problem: I have a value that is set in $record, for instance 1.69. Then

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.