With using
, an alias for a type can be defined and a unit of this type has the same size like an original instantation (1). A template however only weights 1 byte (2).
using aliasOfInt = int;
template<typename T>
struct intWrapper {
using wrappedAliasOfInt = T;
};
int main() {
// (1) --> 4B
std::cout << "sizeof(aliasOfInt) = " << sizeof(aliasOfInt) << std::endl;
// (2) --> 1B
std::cout << "sizeof(intAliasWrapper) = " << sizeof(intWrapper<int>) << std::endl;
}
In (2), is there a real type compiled or why is this only 1B?
The template is a red herring. It's a structure without data members, regardless of T
. Since C++ does not allow sizeof(anything)==0
, sizeof(intWrapper<int>)
has to be greater than 0. This leaves size 1 as a natural choice.