Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Can I conditionally include class members without using #ifdef?

+2
−0

#ifdef sections can, of course, be used to include or exclude chunks of code based on some criteria known at compile time. One big problem is that when the condition is evaluated to false, the chunk is not only excluded from the compiled code but the compiler skips right over it altogether. This means code in that skipped chunk won't go through the compiler's checks to determine whether it's still valid.

For the contents of a function, it's possible to get the best of both worlds using if constexpr. If it evaluates to false the compiler will exclude the body of the statement, but will still check that it's valid.

Is there any way to do this with member variables, though?

Say I have a Widget class that may or may not need a FooBar depending on whether or not we want that feature in one of the many products that uses Widget. I can do this:

class Widget
{
public:
  Widget();
  void doSomething();
  void doSomethingElse();

#ifdef HAS_FOOBAR
  void activateFooBar();
#endif

private:
#ifdef HAS_FOOBAR
  FooBar fooBar;
#endif
};

The advantage is that when I'm building the code for a product that doesn't have a FooBar, my Widget class doesn't have methods it doesn't need, and will also take up less memory due to the member variable being gone. But the disadvantage is that if I'm working on something for a product that doesn't have a FooBar, if I make a change that would break the code in activateFooBar I might miss it entirely until the next time someone builds for a product that does have a FooBar.

If HAS_FOOBAR were a constexpr bool, instead of excluding activateFooBar I could maybe implement it like this:

void Widget::activateFooBar()
{
  if constexpr (!HAS_FOOBAR)
    return;

  // ...
}

but then I still have the fooBar member, no matter what, and instances of the Widget class will always include it in the memory they take up.

Is there any nice, neat way to avoid this?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Use inheritance instead (1 comment)

1 answer

+2
−0

There's nothing as elegant as if constexpr, unfortunately. However, it is possible to achieve the practical effects (member functions and data only present conditionally).

Start by creating a class template that will encapsulate all the fooBar-specific code and data. Use the Curiously Recurring Template Pattern (CRTP) to make the rest of the Widget class accessible:

template <class Self>
struct Widget_FooBar
{
  void activateFooBar()
  {
    fooBar.activate();
    self.doSomething();
  }

private:
  Self& self() { return static_cast<Self&>(*this); }
  const Self& self() const { return static_cast<const Self&>(*this); }

  FooBar fooBar;
};

Next, create an empty class to use as an alternative when FooBar is not supposed to be used:

struct Widget_NoFooBar
{};

Finally, choose the appropriate base class for Widget:

constexpr bool HAS_FOOBAR = whatever;

struct Widget : std::conditional_t<HAS_FOOBAR, Widget_FooBar<Widget>, Widget_NoFooBar>
{
  void doSomethingElse()
  {
    do_stuff();
    if constexpr(HAS_FOOBAR) {
      activateFooBar();
    }
  }
};

This way, the member functions and data are held in Widget_FooBar, which is only included in Widget if HAS_FOOBAR is true.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »