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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:07:11+00:00 2026-05-18T02:07:11+00:00

I would like to trigger a specific constructor for a member variable based on

  • 0

I would like to trigger a specific constructor for a member variable based on flags I pass to the containing class’s constructor.

It’s easiest if I start with a trivial example:

#include <boost/optional.hpp>
#include <boost/none.hpp>
#include <boost/utility/typed_in_place_factory.hpp>

struct state
{
  bool flag1;
  bool flag2;
  int value;
};

class A
{
public:
  A() : _a() {}
  A(boost::none_t none) : _a() {}
  A(state& st) : _a(st.value) {}
  A(const A& copy) : _a(copy._a) {}

private:
  boost::optional<int> _a;
};

class B
{
public:
  B() : _b() {}
  B(boost::none_t none) : _b() {}
  B(state& st) : _b(st.value) {}
  B(const B& copy) : _b(copy._b) {}

private:
  boost::optional<int> _b;
};

class C
{
public:
  C() : _a(boost::none_t()), _b(boost::none_t()) {}
  C(state& st) :
    _a(st.flag1 ? st : boost::none_t()),
    _b(st.flag2 ? st : boost::none_t())
  {}

private:
  boost::optional<A> _a;
  boost::optional<B> _b;
};

int main(void)
{
  state f = { true, false, 10 };
  C c(f);

  return 0;
}

So the idea is to trigger the constructor for A with state, but B with boost::none_t.
The above code does not compile because the ternary operator is expecting the same type for both possibilities, and state and boost::none_t are not the same type.
Can anyone think of an elegant way around this?

I know of two solutions:

  1. Copy construct, i.e.

    _a(st.flag1 ? A(st) : A(boost::none_t())),
    _b(st.flag2 ? B(st) : B(boost::none_t()))

  2. Use pointers instead of boost::optional<>, then

    _a(st.flag1 ? new A(st) : new A(boost::none_t())),
    _b(st.flag2 ? new B(st) : new B(boost::none_t()))

#2 is not so appealing, as I’m trying to avoid dynamic memory allocation (the real example has tens of members and deeply nested structures with more members).
#1 is also not appealing as I would need to construct and then copy.

Is there a better alternative?

EDIT: state is modified by the members on construction, so I don’t want to construct something with state if the flag is not set.

  • 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-18T02:07:12+00:00Added an answer on May 18, 2026 at 2:07 am

    It appears that using boost::optional<> is complicating things, using it with the copy constructed approach seems to cause both the state based and copy based constructors of A & B to be called – even with full optimization.

    However, if I define the members of C as simply A _a and B _b, the copy constructor is actually optimised away, so doing

    _a(st.flag1? A(st) : A()), _b(st.flag2? B(st) : B())
    

    Is actually apparently the most optimal way of getting this to work, I just have to workaround the other functionality I was using from boost::optional<> (i.e. initialized vs. uninitialized).

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

Sidebar

Related Questions

I would like to create a small Windows Messenger Client that I can trigger
I would like to test a string containing a path to a file for
I have a jquery fullcalendar . I would like to trigger jquery QTip (or
I would like to validate the textbox for specific text and it must not
I would like to know if it is possible to have a trigger when
I would like to trigger the onresize event from my C# code behind. I
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to sort an array in ascending order using C/C++ . The
I would like to have a reference for the pros and cons of using

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.