xmh0511
xmh0511

Reputation: 7369

what are lookup rules when a name occured before function's declarator-id?

#include <iostream>

typedef int Name;
Name func(int){
  return 0;
}

int main(){
}

Consider the above code, I can't find a bullet in [basic.lookup.unqual] that can interpret how to lookup Name for definition of func. I will cite some potential rules here:

  1. A name used in global scope, outside of any function, class or user-declared namespace, shall be declared before its use in global scope.

  2. A name used in a user-declared namespace outside of the definition of any function or class shall be declared before its use in that namespace or before its use in a namespace enclosing its namespace.

  3. In the definition of a function that is a member of namespace N, a name used after the function's declarator-id shall be declared before its use in the block in which it is used or in one of its enclosing blocks ([stmt.block]) or shall be declared before its use in namespace N or, if N is a nested namespace, shall be declared before its use in one of N's enclosing namespaces

Please note the emphasized parts, it seems that my case does not satisfy these bullets, because Function definitions have the form

function-definition:

attribute-specifier-seq(opt) decl-specifier-seq(opt) declarator virt-specifier-seq(opt) function-body

Let me analyze the first bullet. It says outside of any function,but according to the Function definitions rule, I think that Name is within the function(definition), bullet 1 isn't satisfied. Bullet 2 is similar with that of bullet 1. Bullet 3 says that the name used after the function's declarator-id, in my case, the Name is used before the function's declarator-id. So what's the rule about this case to find the unqualified name Name?

My confusions:

In my example ,the Name,func ,(int) and { return 0;}(function body) are all parts of that function definition,So:

  1. what is outside of any function,such as the func in my example,where's area of outside of that function?
  2. what is outside of the definition of any function,such as the func definition in my example,where's area of outside of the definition of that function?

Upvotes: 2

Views: 182

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473437

I think that Name is within the function(definition), bullet 1 isn't satisfied.

Bullet 1 didn't say "function(definition)". It said "outside of any function". It doesn't specify declaration or definition; merely "outside of any function".

Since being inside or outside of a "function" is not a defined concept, it must be read as plain English. Is a function prototype "outside of the function"? Visually speaking, there's nothing special about a function prototype that is suggestive of an inside/outside distinction. By contrast, the function body's block scope does suggest an inside/outside distinction.

The intent of the text of course is quite obvious; it's talking about the function body. The rule equates "function", "class" and "namespace", all of which have a block that defines scoping for names. That is the most logical place for any inside/outside distinction, so it seems pretty obvious that it's saying that the global scope consists of everything that isn't in the scope of a function body, the scope of a class definition, or the scope of a namespace body.

So this can be easily handled with an editorial change.


Note that the committee recognizes the wording of this section (among others in that area) as somewhat defective, and there's a proposal for rewriting it into something more coherent in C++23. The new wording from the proposal completely rewrites this section.

Upvotes: 6

Related Questions