Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Name lookup

      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
       
       

      Name lookup is the procedure by which aname, when encountered in a program, is associated with thedeclaration that introduced it.

      For example, to compilestd::cout<<std::endl;, the compiler performs:

      • unqualified name lookup for the namestd, which finds the declaration of namespace std in the header<iostream>
      • qualified name lookup for the namecout, which finds a variable declaration in the namespacestd
      • qualified name lookup for the nameendl, which finds a function template declaration in the namespacestd
      • bothargument-dependent lookup for the nameoperator<<, which finds multiple function template declarations in the namespacestd, and qualified name lookup for the namestd::ostream::operator<<, which finds multiple member function declarations in classstd::ostream.

      For function and function template names, name lookup can associate multiple declarations with the same name, and may obtain additional declarations fromargument-dependent lookup.Template argument deduction may also apply, and the set of declarations is passed tooverload resolution, which selects the declaration that will be used.Member access rules, if applicable, are considered only after name lookup and overload resolution.

      For all other names (variables, namespaces, classes, etc), name lookup can associate multiple declarations only if they declare the sameentity, otherwise it must produce a single declaration in order for the program to compile. Lookup for a name in a scope finds all declarations of that name, with one exception, known as the "struct hack" or "type/non-type hiding": Within the same scope, some occurrences of a name may refer to a declaration of a class/struct/union/enum that is not atypedef, while all other occurrences of the same name either all refer to the same variable, non-static data member, or enumerator, or they all refer to possibly overloaded function or function template names. In this case, there is no error, but the type name is hidden from lookup (the code must useelaborated type specifier to access it).

      [edit]Types of lookup

      If the name appears immediately to the right of the scope resolution operator:: or possibly after:: followed by the disambiguating keywordtemplate, see

      Otherwise, see

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 2063C++98"struct hack" did not apply in class scope (breaks C compatibility)applied
      CWG 2218C++98lookup for non-function (template) names could not associate
      multiple declarations, even if they declare the same entity
      allowed

      [edit]See also

      C documentation forLookup and name spaces
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/lookup&oldid=175783"

      [8]ページ先頭

      ©2009-2025 Movatter.jp