General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
|
Unless otherwise specified, two declarations cannot (re)introduce the same entity. The program is ill-formed if such declarations exist.
Contents |
Two declarationscorrespond if they (re)introduce the same name, both declare constructors, or both declare destructors, unless
Twofunction declarations declarecorresponding overloads if both declare functions satisfying all following conditions:
| (since C++20) |
| (since C++23) |
Twofunction template declarations declarecorresponding overloads if both declare function templates satisfying all following conditions:
| (since C++20) |
| (since C++23) |
struct A{friendvoid c();// #1}; struct B{friendvoid c(){}// corresponds to, and defines, #1}; typedefint Int; enum E:int{ a}; void f(int);// #2void f(Int){}// defines #2void f(E){}// OK, another overload struct X{staticvoid f();void f()const;// error: redeclaration void g();void g()const;// OKvoid g()&;// error: redeclaration void h(this X&,int);void h(int)&&;// OK, another overload void j(thisconst X&);void j()const&;// error: redeclaration void k();void k(this X&);// error: redeclaration};
A declaration isname-independent if its name is_ and it declares
| (since C++26) |
Unless otherwise specified, two declarations of entitiesdeclare the same entity if all following conditions are satisfied, considering declarations of unnamed types to introduce theirtypedef names andenumeration names for linkage purposes (if any exists):
| (since C++26) |
| (since C++20) |
A declaration of an entity or typedef nameX
is aredeclaration ofX
if another declaration ofX
is reachable from it; otherwise, it is afirst declaration ofX
.
If any two declarations of an entityE
violate the corresponding restriction below, the program is ill-formed:
E
to be a variable, the other must also declareE
as a variable of the same type.E
to be afunction, the other must also declareE
as a function of the same type.E
to be anenumerator, the other must also declareE
as an enumerator.E
to be anamespace, the other must also declareE
as a namespace.E
to be aclass type, the other must also declareE
as a class type.E
to be anenumeration type, the other must also declareE
as an enumeration type.E
to be aclass template, the other must also declareE
as a class template with an equivalent template parameter list (seefunction template overloading).E
to be afunction template, the other must also declareE
as a function template with an equivalent template parameter list and type.
| (since C++11) |
| (since C++14) |
| (since C++20) |
Types are compared after all adjustments of types (during whichtypedefs are replaced by their definitions). Declarations for an array object can specify array types that differ by the presence or absence of a major array bound. No diagnostic is required if neither declaration is reachable from the other.
void g();// #1void g(int);// OK, different entity from #1 (they do not correspond)int g();// Error: same entity as #1 with different type void h();// #2namespace h{}// Error: same entity as #2, but not a function
If a declarationH
that declares a name withinternal linkage precedes a declarationD
in another translation unitU
and would declare the same entity asD
if it appeared inU
, the program is ill-formed.
Two declarationspotentially conflict if they correspond but declare different entities.
If, in any scope, a name is bound to two declarationsA
andB
that potentially conflict,B
is not name-independent(since C++26), andA
precedesB
, the program is ill-formed:
void f(){int x, y;void x();// Error: different entity for xint y;// Error: redefinition} enum{ f};// Error: different entity for ::f namespace A{}namespace B= A;namespace B= A;// OK, no effectnamespace B= B;// OK, no effectnamespace A= B;// OK, no effectnamespace B{}// Error: different entity for B void g(){int _; _=0;// OKint _;// OK since C++26, name-independent declaration _=0;// Error: two non-function declarations in the lookup set} void h(){int _;// #1 _++;// OKstaticint _;// Error: conflicts with #1 because// static variables are not name-independent}
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 279 (P1787R6) | C++98 | it was unclear whether an unnamed class or enumeration can be redeclared if it has a typedef name for linkage purposes | it can be redeclared |
CWG 338 (P1787R6) | C++98 | it was unclear whether an unnamed enumeration can be redeclared if it has an enumerator as a name for linkage purposes | it can be redeclared |
CWG 1884 (P1787R6) | C++98 | the restrictions applied to multiple declarations of the same entity were unclear | made clear |