3

After enabling lto in one of my projects the compiler started to throw warnings for lto violations at me. After a bit of testing it boils down to this construct.

typedef struct {
    typedef struct {
    } test_t;
    std::vector<test_t> t;
} test2_t;

Once I change it to

struct test2_t {
    typedef struct {
    } test_t;
    std::vector<test_t> t;
};

or

typedef struct {
    typedef struct {
    } test_t;
    test_t t;
} test2_t;

the warning goes away but I don't really understand why. My guess is that it treats the anonymous struct as different types in different files, but I would not expect a warning since it is a common use case. Note that it does not make a difference if I change the inner typedef but leave the outer on in place. It also does not matter whether test_t contains members or not.

  • What's the reason for this warning?
  • Is this behaviour expected?
  • Might this be an STL error, since it works fine without vector?
Thalhammer
  • 285
  • 3
  • 7
  • 3
    C developers use the `typedef struct` approach. C++ developers need not to. Even if they need to, they should prefer `using` to `typedef`. – Ron Sep 08 '18 at 10:26
  • @Ron While I absolutely agree with this point, this project switched from plain C to C++ only recently and most of the code was (not yet) migrated. – Thalhammer Sep 08 '18 at 10:34
  • The compiler warns because it is confused. It is slightly smarter than average C++ developer so it stops warning where average C++ developer is still confused. – Öö Tiib Sep 08 '18 at 10:40
  • @asynts For some reason, I failed to reproduce it in such a simple case as well, but once it is used from multiple (separately compiled) and then linked files they somehow appeared. This only happens once I enabled link-time optimizations. – Thalhammer Sep 08 '18 at 11:26
  • I think that it hasn't yet realized at the point of use of the nested struct name, that the nesting struct has a name-for-linkage-purposes. It looks like a bug. – Johannes Schaub - litb Sep 08 '18 at 11:34
  • The details of these linkage issues are pretty complex though, so I'll leave it to someone else to decide what's the difference between each of your examples. – Johannes Schaub - litb Sep 08 '18 at 11:42
  • @asynts I managed to come up with a minimal testcase and put it up in a gist [here](https://gist.github.com/Thalhammer/cd743d519721fd45bd86b559c9fb844f). – Thalhammer Sep 08 '18 at 11:47
  • @JohannesSchaub-litb Maybe the minimal testcase [here](https://gist.github.com/Thalhammer/cd743d519721fd45bd86b559c9fb844f) helps. – Thalhammer Sep 08 '18 at 11:48
  • Which compiler/linker are you using for LTO, and which version, on what platform? – Eljay Sep 08 '18 at 12:47
  • @JohannesSchaub-litb: This is tangential to this question — please mark this comment 'no longer needed' when you've read it (or respond to me and I'll remove it). Someone removed the [tag:c++-faq] tag from the question [What is the strict aliasing rule?](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) leaving only the [tag:c] tag. Is that an appropriate change (given that several of the answers do not address C at all and do address C++ versions up to C++17)? – Jonathan Leffler Oct 17 '18 at 00:15

0 Answers0