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.

Comments on Can I conditionally include class members without using #ifdef?

Post

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 attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Use inheritance instead (1 comment)
Use inheritance instead
Lundin‭ wrote 6 months ago

The correct way to solve your actual problem is most likely with proper program design. Avoid preprocessor conditionals and use inheritance instead. "Widget without foobar" is either the parent or the child of the class "Widget with foobar". One could be abstract if it makes sense, or implement an empty function which does nothing. Etc etc.