开发者

What is meant by 'use of a function'

开发者 https://www.devze.com 2023-01-13 07:23 出处:网络
$3.6.1/3 states- \"The function main shall not be used (3.2) within a program.\". The sample academically motivated program below 开发者_JAVA技巧uses the name \'main\' in a couple of ways which I

$3.6.1/3 states-

"The function main shall not be used (3.2) within a program.".

The sample academically motivated program below 开发者_JAVA技巧uses the name 'main' in a couple of ways which I thought are legitimate. This is based on the assumption that 'usage of a function' is related to calling the function (directly/indirectly).

struct MyClass{ 
private: 
   MyClass(){}  
   friend int main(); 
};   

int main(){  
   MyClass m;  
   int (*p)() = main; // but don't do anything 
}
  1. Fair enough, the code shown compiles with gcc/VS 2010.

  2. I am surprised by Comeau's error.

Comeau online gives error while declaration 'p' (i.e while taking address of 'main') but not while declaraing 'main' as a friend.

Who/What is right with respect to C++03?


C++03 §3.2/2 says:

An object or non-overloaded function is used if its name appears in a potentially-evaluated expression.

It goes on to list what constitutes use of other various types of entities; this is the important one here.

A friend declaration is not an expression.

When the function main() is converted to a pointer and that pointer is assigned to p, that is an expression and it is potentially evaluated (C++03 §3.2/2):

An expression is potentially evaluated unless it appears where an integral constant expression is required (see 5.19), is the operand of the sizeof operator (5.3.3), or is the operand of the typeid operator and the expression does not designate an lvalue of polymorphic class type (5.2.8).


C++03 Appendix C.1 on C++ and ISO C Compatibility says:

Changes in 3.6

Change: Main cannot be called recursively and cannot have its address taken
Rationale: The main function may require special actions.
Effect on original feature: Deletion of semantically well-defined feature
Difficulty of converting: Trivial: create an intermediary function such as mymain(argc, argv.
How widely used: Seldom


Going by the exact wording ("shall not be used"), I would think that the first example is considered legal because it doesn't use the function in any way. It adds some kind of reference to it somewhere and allows the function to access the private data. It doesn't use main(), it provides more access to it.

The second example (taking the address) actually uses main() as a symbol, by taking the address of it and placing that into a pointer to a function. Not only will that allow you to easily break the rule of not calling main from within the program, it has to interact with it (at least the info that tells where main is at).


I think that in the first case, the friend function just refers to "some function called main" while in the second case its rather "oh, you mean THE ONE main? I can't allow that".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号