[19] q[extern] idiosyncracies

Despite my numerous summaries, I could never reach the bottom of this keyword.

http://www.goldsborough.me/c/c++/linker/2016/03/30/19-34-25-internal_and_external_linkage_in_c++/ has good details like

int x;          // is the same as
extern int x{}; // which will both likely cause linker errors if placed in header.

extern int x;   // while this only declares the integer, which is ok.
Advertisements

Empty base-class optimization

This optimization only zeros out the runtime [1] size of baseclass subobject [2]. All other sizes of empty types are the same — one byte.

Suppose Der subclasses B1 and B2.

Note on [1]:
If B1 is empty, then the compile-time operator sizeof() shows sizeof(B1) == 1 not zero, but at run time, the Der object size shows the B1 sub-object has zero size, due to EBCO.

Note on [2]:
If all of Der, B1 and B2 are empty, we know sizeof(Der) == 1, but how about rutime sizeof(an_Der_instance)? Also one, not zero, because this instance is not a baseclass subobject.

use STL Map to emulate a Set#as JDK does

Q1: is it practical to use a STL Map to emulate the Set class-template, at comparable efficiency?

We know a Map stores PAIRs in a red-black tree. Can we just put an empty thing in the PAIR.second field?

Q2: how small can the “PAIR.second” footprint be? 0 byte? 1 byte? Java is less memory efficiency … where everything is heapy thingy..
%%A: i don’t think it can be 0 byte. sizeof(PAIR) is known when you specialize the PAIR template with T1 and T2. sizeof(PAIR) depends on sizeof(T1) + sizeof(T2). Compiler needs to know sizeof(PAIR) in order to allocate memory to a new PAIR object.

Q3: is there any type T2 such that sizeof(T2) == 0?
%%A: I don’t know any.

Q4: we know (confirmed) java HashSet is implemented using HashMap physically. Is that different from C++?
A: different

Q5: java TreeSet.java source code shows a physical implementation using a map. Is that different from STL?
A: different