Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen do programmers use Empty Base Optimization (EBO)
    primarykey
    data
    text
    <p>I was reading about Empty Base Optimization(EBO). While reading, the following questions popped up in my mind:</p> <ol> <li><p>What is the point of using Empty class as base class when <strong>it contributes nothing to the derived classes (neither functionality-wise, nor data-wise)</strong>?</p></li> <li><p>In <a href="http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=319">this article</a>, I read this:</p></li> </ol> <blockquote> <p>//S is empty<br/> class struct T : S <br/> {<br/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int x;<br/> };</p> <p>[...]</p> <p>Notice that we didn’t lose any data or code accuracy: when you create a standalone object of type S, the object’s size is still 1 (or more) as before; only when S is used as base class of another class does its memory footprint shrink to zero. To realize the impact of this saving, imagine a vector that contains 125,000 objects. <strong>The EBO alone saves half a megabyte of memory!</strong></p> </blockquote> <p>Does it mean that if we don't use "S" as base class of "T", we would <strong>necessarily</strong> consume double of megabyte of memory? I think, the article compares two different scenarios which I don't think is correct.</p> <p>I would like to know a real scenario when EBO can proven to be useful.(means, in the same scenario, we would <em>necessarily</em> be at loss IF we don't use EBO!).</p> <h1>Please note that if your answer contains explanations like this :</h1> <p>The whole point is that an empty class has non-zero size, but when derived or deriving it can have zero size, then I'm NOT asking that, as I know that already. My question is, why would anyone derive his class from an empty class in the first place? <strong>Even if he doesn't derive and simply writes his class (without any empty base), is he at loss in ANY way?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload