Tuple DIP

Atila Neves atila.neves at gmail.com
Tue Jan 23 16:30:11 UTC 2018


On Tuesday, 23 January 2018 at 14:58:07 UTC, Petar Kirov 
[ZombineDev] wrote:
> On Tuesday, 23 January 2018 at 11:04:33 UTC, Atila Neves wrote:
>> On Sunday, 14 January 2018 at 18:17:38 UTC, Timon Gehr wrote:
>>> On 14.01.2018 19:14, Timothee Cour wrote:
>>>> actually I just learned that indeed 
>>>> sizeof(typeof(tuple()))=1, but why
>>>> is that? (at least for std.typecons.tuple)
>>>> maybe worth mentioning that in the DIP (with rationale)
>>>
>>> It's inherited from C, where all struct instances have size 
>>> at least 1. (Such that each of them has a distinct address.)
>>
>> Inherited from C++. In C empty structs have size 0. This 
>> caused me all sorts of problems when importing C headers from 
>> C++ in funky codebases.
>>
>> foo.c:
>> #include <stdio.h>
>>
>> struct Foo {};
>>
>> int main() {
>>     printf("%zu\n", sizeof(struct Foo));
>>     return 0;
>> }
>>
>>
>> % clear && gcc foo.c && ./a.out
>> 0
>>
>> % clear && gcc -xc++ foo.c && ./a.out
>> 1
>>
>>
>> Atila
>
> AFAIR the ISO C standard does not allow empty structs (as they 
> would have no meaning). If you use the warnings as errors 
> option, it won't compile:
>
> <source>:3:8: error: struct has no members [-Werror=pedantic]
>  struct Foo {};
>         ^~~

That's a warning treated as error. I checked and it seems that 
you're right about the C standard, although in practice compilers 
seem to accept empty structs. I knew that in C++ it's explicit 
that empty classes have size 1. Live and learn.

Atila


More information about the Digitalmars-d mailing list