13

C++ type fuxor we found by accident in our code. I am not upset about allowing 2 different types to have the same type name. I am upset that the compiler thinks they are the same type and allows pointer assignment. I didn't know you could fool the compiler this way. I suppose this might be useful if the types are in sync. But damn, this is kinda fucked.

Comments
  • 4
    So I went on a walk and thought about this some more. I can see this being useful if you have a private class vs a public class. As long as you ensure the data sizes match and the variables match you should be able to create interchangeable objects with different methods available depending upon context. This could be hazardous to maintain though.
  • 6
    @Demolishun

    https://en.m.wikipedia.org/wiki/...

    Important to read... Undefined behaviour can be pretty annoying.
  • 2
    @IntrusionCM yeah, I saw that. cppreference has a better explanation. I am trying to fathom why the compiler isn't enforcing this. Either it is hard to catch or it is actually useful in some contexts.
  • 5
    @Demolishun the compiler can't enforce it unless one compilation unit includes both declarations. If you're only using forward declarations, no help there. The linker might be able to catch it, but that's not so straight forward. The same object can correctly be declared in multiple compilation units, part of what a linker does is spots that and removes the duplication.
  • 3
    @atheist yeah, I forget the comp units are like independent islands.
  • 5
    @Demolishun yes, and someone will have (ab)used that todo some conversion do changing it will be a breaking change, hence very very hard to get accepted.
  • 1
    @Voxera "modules", still barely used but 100% the future
  • 1
    Yeah, was gonna say it was the forward declaration, but got beaten to it.
  • 0
    Last I used C++ was 6 years ago. Doesn't everyone use C++ modules by now?
  • 0
    @jassole

    Problem with c++ is that the language evolves faster than the adoption, particularly for multi platform projects due to differences in speed of feature adoption (looking at you msvc, tho they have improved drastically lately).

    Also the massive support codebase which could take ages to update.
  • 0
    @CoreFusionX fair point. I don't miss C++ at all thankfully.
  • 1
    @jassole we are mostly still on C++11. I have one project I am compiling with C++17. Now I have to backport it to an older compiler to support an older system. Not looking forward to that.
  • 1
    @Demolishun

    Fortunately, boost does a very decent job at backporting most modern features to older versions.
Add Comment