What is the purpose of standard-layout guarantees for "black box" types?
Asked Answered
C

2

6

The C++ standard specifies that mutex, atomics or conditinal_variable are of standard-layout type.

What is the benefit of this specification? How a user can take advantage of this property?

And in general, what could I gain if a know a type is standard-layout without knowing the detail of its implementation?

Culminant answered 27/9, 2017 at 13:20 Comment(2)
A standard-layout type can be more readily consumed by other languages.Bohon
@Bohon Even when the other language is also C++? ;)Battement
S
1

You could make your code talk with other programs, written in different Programming Languages than yours.

The ref mentions C++ concepts: StandardLayoutType:

Standard layout types are useful for communicating with code written in other programming languages.

Subjacent answered 27/9, 2017 at 13:23 Comment(6)
You could use std::offsetof \o/Bills
You mean this @Bills question?Subjacent
It seems that I can not know if there is a padding between members of standard-layout class. Are compiler allowed to add padding between members even if alignment would not require it?Culminant
@Culminant this question can help!Subjacent
@Subjacent not specifically, but it applies.Bills
Same comment as for ProgrammerDude it would help me if you gave me an exemple, where one use a standard-layout struct to communicate with an other programCulminant
B
2

From this standard layout reference:

Standard layout types are useful for communicating with code written in other programming languages.

For example, if you build a mixed C and C++ application, the C structures will be standard layout and can be used interchangeably between the parts written in C and the parts written in C++. This is often very crucial for being able to use operating system native functions and structures.

Baileybailie answered 27/9, 2017 at 13:24 Comment(6)
Nice answer, glad we agree! =)Subjacent
I honestly do not see how, it would help me if you gavee me an exemple?Culminant
@Culminant There are many libraries written for C. What if you want to use one of those libraries in your C++ application? What is that C library have a structure? Because that structure will be standard layout you can create instance of it in your C++ program, and pass pointers to it (or even copies of instances) to the C library functions.Baileybailie
@Someprogrammerdude But mutex is a C++ structure, so can I pass a mutex to a C library, otherwise what is the point of having mutex standard-layout?Culminant
@Culminant So your question isn't about standard layout in general, but about why just those structures are specified as standard layout? Perhaps you should narrow down your question then?Baileybailie
@Someprogrammerdude Actualy StoryTeller did it a few hour ago. He has narrowed the question to "black-boxes" types. Then I am not sure if I have to precise that the struct is declared in a C++ header file, since my question is about C++?Culminant
S
1

You could make your code talk with other programs, written in different Programming Languages than yours.

The ref mentions C++ concepts: StandardLayoutType:

Standard layout types are useful for communicating with code written in other programming languages.

Subjacent answered 27/9, 2017 at 13:23 Comment(6)
You could use std::offsetof \o/Bills
You mean this @Bills question?Subjacent
It seems that I can not know if there is a padding between members of standard-layout class. Are compiler allowed to add padding between members even if alignment would not require it?Culminant
@Culminant this question can help!Subjacent
@Subjacent not specifically, but it applies.Bills
Same comment as for ProgrammerDude it would help me if you gave me an exemple, where one use a standard-layout struct to communicate with an other programCulminant

© 2022 - 2024 — McMap. All rights reserved.