vendredi 1 juillet 2016

What is the intention of 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.

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

a.cpp

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;
}

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

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

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.

What else?

Aucun commentaire:

Enregistrer un commentaire