What is the intention of ODR?

后端 未结 4 717
囚心锁ツ
囚心锁ツ 2021-01-06 16:11

I do understand what ODR says, but I don\'t understand what it tries to achieve.

I see two consequences of violating it - user will get syntax error, which is totall

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-06 16:33

    When the function expects to get one of these structs, and you redeclare it as something different, which struct does that function receive, and how? Remember, C++ is static, so if you send a struct in by value, the function must know the structure of it. Because C++ is type-safe, allowing violation of the ODR would violate this type safety.

    Most importantly, what would be the gain in the lack of the ODR? I can think of hundreds of things that it would make harder to not have it, and nothing to gain. There is literally no flexibility to be acheived from being able to stomp previously declared types in the same namespace. At the very best, it just would make multiple inclusion not require header guards, which is a very minimal gain at best.

提交回复
热议问题