mercredi 29 juillet 2015

multiple declarations in C++

In [basic.scope.declarative]p4, one reads

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name, — (4.1) they shall all refer to the same entity …

A naïve reading might imply that the following code might be valid because "both declarations refer to the same entity"

int x;
int x;

One then might remember the one definition rule [basic.def.odr]p1. The above reasoning might apply only to declarations not to definitions. The distinction is spelled out in [basic.def]p2. For example, the following code is certainly valid:

extern int x;
extern int x;

The last example in [basic.def]p2 suggests then that the following code should be valid, but it does not compile (using MSVC2015).

struct B
{
    int y;
};

struct D : B
{
    using B::y;
    using B::y;
};

Where is the problem?


The error message is

the using-declaration for 'B::y' cannot co-exist with the existing using-declaration for 'B::y'

Aucun commentaire:

Enregistrer un commentaire