ODR的目的是什么? [英] What is the intention of ODR?

查看:394
本文介绍了ODR的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实了解ODR所说的话,但是我不明白它试图达到什么目的.

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 totally fine. And also there may be some fatal errors, and again the user would be the only one who's guilty.

作为违反ODR并得到一些致命错误的示例,我想像这样:

As example of violating ODR and getting some fatal error I imagine like this:

struct A
{
        int a;
        double b;
};
void f(A a)
{
        std::cout << a.a << " " << a.b << std::endl;
}

main.cpp

struct A
{
        int a;
        int b;

};
void f(A a);

int main()
{

        A a = {5, 6};
        f(a);

        return 0;
}

如果示例与ODR无关,请纠正我.

Please correct me if example isn't related to ODR.

那么,ODR是否试图禁止用户做这种有害的事情?我不这么认为.

So, is ODR trying to prohibit the user from doing such harmful things? I don't think so.

它是否正在尝试为编译器编写者设置一些规则,以避免违反它的潜在危害?可能不是,因为大多数编译器不会检查ODR违规情况.

Is it trying to set some rules for compiler writers, to avoid potential harms from violating it? Probably no, since most of compilers don't check of ODR violation.

还有什么?

推荐答案

ODR指示格式正确的C ++程序.违反ODR意味着您的程序格式不正确,并且标准不指示程序将执行什么操作,是否应进行编译等.通常,将ODR违反标记为无需诊断",以使编译器编写者的工作更加容易.

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.

这允许C ++编译器对您提供的代码进行某些简化的假设,例如::A在任何地方都是相同的结构类型,而不必在每次使用时都进行检查.

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.

编译器可以自由地获取您的代码并将其编译为c:格式.或其他任何东西.可以免费检测ODR违规,并使用它来证明代码分支无法运行,并消除导致该分支的路径.

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.

这篇关于ODR的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆