What is the intention of ODR?

后端 未结 4 716
囚心锁ツ
囚心锁ツ 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:17

    The ODR dictates what C++ programs are well formed. A ODR violation means your program is ill-formed, and the standard does not dictate what the program will do, if it should compile, etc. Mostly ODR violations are marked "no diagnostic required" to make the job of the compiler writer easier.

    This permits the C++ compiler to make certain simplifying assumptions about the code you feed it, like that ::A is the same struct type everywhere, and not have to check at each point of use.

    The compiler is free to take your code and compile it to format c:. Or anything else. It is free to detect ODR violations, and use it to prove that branch of code cannot run, and eliminate paths that lead there.

提交回复
热议问题