4 ms·
Personally, I think this is blown out of proportion a bit - There are things to complain about, but LTO problems are really only caused by other issues. But wit
by DSMan195276 11y ago
Personally, I think this is blown out of proportion a bit - There are things to complain about, but LTO problems are really only caused by other issues. But with that said, what you described isn't really a worry:
LTO is done on the compilers internal representation of the code (IE. GIMPLE or LLVM IR). This representation is generated based on the rules of each language, and optimizations are performed on this representation instead of the original source representation. Both C and C++ (and anything else) are converted into these representations. LTO simply keeps the GIMPLE or LLVM IR around until link time, and then when the program is linked optimizations are performed over the entire representation of the program. Crossing the language barrier shouldn't be a problem, because the GIMPLE has it's own rules to follow to make sure everything still functions the same. Once you reach this point both languages are already compiled in the practical sense, they're just not actual machine code yet. I would expect however, that because of the differences there are far less optimization opportunities to be taken advantage of.
- haberman 11y agoOk let's look at a specific example then. // foo.c #include <stdlib.h> typedef struct { int x; } s; s *make_s() { s *ret = malloc(sizeof(*ret)); ret->x = 5; return ret; } void free_s(s *val) { free(val); } // bar.cc // This class is standard-layout and matches "s" from C. class C { int getX() { return x_; } private: int x_; } extern C* make_s(); extern void free_s(C* c); int main(void) { C* c = make_s(); int ret = c->getX(); free_s(c); return ret; } C++ says you can only call a method on an object whose lifetime has begun. Can C begin the lifetime of a C++ object?
- Kristine1975 11y ago> Can C begin the lifetime of a C++ object? Yes. C++14 Standard §3.8p1: ...An object is said to have non-vacuous initialization if it is of a class or aggregate type and it or one of its members is initialized by a constructor other than a trivial default constructor... The lifetime of an object of type T begins when: — storage with the proper alignment and size for type T is obtained, and — if the object has non-vacuous initialization, its initialization is complete. Since class "C" is standard-layout and has a trivial default constructor (in this case none at all), assuming that malloc allocates memory that is suitably aligned for "C", the function make_s returns a pointer to an instance of C++ class "C" whose lifetime has begun. Edit: Thanks for asking btw. I use similar code in a project of mine but never actually checked whether it is actually standard-conforming.
- deleted 11y ago[deleted]