Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Conflicting declarations

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      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
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      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
       
      Declarations
       

      Unless otherwise specified, two declarations cannot (re)introduce the same entity. The program is ill-formed if such declarations exist.

      Contents

      [edit]Corresponding declarations

      Two declarationscorrespond if they (re)introduce the same name, both declare constructors, or both declare destructors, unless

      • either is ausing declaration,
      • one declares a type (not atypedef name) and the other declares a variable, non-static data member other than of ananonymous union, enumerator, function, or function template, or
      • each declares a function or function template and they do not declare corresponding overloads.

      [edit]Corresponding function overloads

      Twofunction declarations declarecorresponding overloads if both declare functions satisfying all following conditions:

      (since C++20)
      • If both of them are non-static member functions, they need to additionally satisfy one of the following requirements:
      • Exactly one of them is animplicit object member function without ref-qualifier and the types of their object parameters, after removing top-level references, are the same.
      (since C++23)
      • Their object parameters have the same type.

      [edit]Corresponding function template overloads

      Twofunction template declarations declarecorresponding overloads if both declare function templates satisfying all following conditions:

      • Their corresponding template parameters are either both declared withoutconstraint, or both declared with equivalent constraints.
      • They have equivalent trailingrequires clauses (if any).
      (since C++20)
      • If both are non-static members function templates, they need to additionally satisfy one of the following requirements:
      • Exactly one of them is animplicit object member function template without ref-qualifier and the types of their object parameters, after removing all references, are equivalent.
      (since C++23)
      • Their object parameters have equivalent types.
      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};

      [edit]Multiple declarations of the same entity

      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):

      • Neither is a name-independent declaration.
      (since C++26)
      • One of the following conditions is satisfied:
      • They appear in the same translation unit.
      (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.

      [edit]Restrictions

      If any two declarations of an entityE violate the corresponding restriction below, the program is ill-formed:

      • If one declaresE to be a variable, the other must also declareE as a variable of the same type.
      • If one declaresE to be afunction, the other must also declareE as a function of the same type.
      • If one declaresE to be anenumerator, the other must also declareE as an enumerator.
      • If one declaresE to be anamespace, the other must also declareE as a namespace.
      • If one declaresE to be aclass type, the other must also declareE as a class type.
      • If one declaresE to be anenumeration type, the other must also declareE as an enumeration type.
      • If one declaresE to be aclass template, the other must also declareE as a class template with an equivalent template parameter list (seefunction template overloading).
      • If one declaresE to be afunction template, the other must also declareE as a function template with an equivalent template parameter list and type.
      • If one declaresE to be analias template, the other must also declareE as an alias template with an equivalent template parameter list andtype-id.
      (since C++11)
      • If one declaresE to be a (partial specialization of a)variable template, the other must also declareE as a (partial specialization of a) variable template with an equivalent template parameter list and type.
      (since C++14)
      • If one declaresE to be aconcept, the other must also declareE as a concept.
      (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.

      [edit]Potentially-conflicting declarations

      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}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 279
      (P1787R6)
      C++98it 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++98it 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++98the restrictions applied to multiple
      declarations of the same entity were unclear
      made clear
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/conflicting_declarations&oldid=181083"

      [8]ページ先頭

      ©2009-2025 Movatter.jp