I came from C programming and have a question about the way to go to solve the following problem in C++.
I'm designing a header file to be used in C program which is supposed to have C++ implementation. It roughly looks as
lib.h
typedef struct some_opaque_struct_ some_opaque_struct;
#ifdef __cplusplus
extern "C" {
#endif
some_opaque_struct *init(int);
#ifdef __cplusplus
}
#endif
lib.cc
struct some_opaque_struct_ {
public:
class ExternalLibraryClass slc;
}
extern "C" some_opaque_struct_* init(int rank){
//some implementation in C++
}
In C it is possible to cast a pointer to initial member of a struct to the pointer to the struct. It is guaranteed by the Standard:
N2310/6.7.2.1
A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.
Is it possible to cast class ExternalLibraryClass *
to struct some_opaque_struct_ *
in a conforming way and if so how to do it. AFAIK usage of C style cast is discouraged in C++.