'使用函数'是什么意思(What is meant by 'use of a function')

编程入门 行业动态 更新时间:2024-10-28 19:35:16
'使用函数'是什么意思(What is meant by 'use of a function')

$ 3.6.1 / 3州 -

“功能主体不得在程序中使用 (3.2)”。

下面这个以学术为动力的样本程序以几种方式使用名称'main',我认为这是合法的。 这是基于'使用函数'与直接/间接调用函数有关的假设。

struct MyClass{ private: MyClass(){} friend int main(); }; int main(){ MyClass m; int (*p)() = main; // but don't do anything }

显示的代码足够公平,可以用gcc / VS 2010进行编译。

我对科莫的错误感到惊讶。

Comeau online在声明'p'时(即,在处理'main'的地址时)给出错误,但在声明'main'为朋友时不给出错误。

谁/对C ++ 03来说什么是正确的?

$3.6.1/3 states-

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

The sample academically motivated program below 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 }

Fair enough, the code shown compiles with gcc/VS 2010.

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

如果对象或非重载函数的名称出现在潜在评估的表达式中,则使用该函数。

它继续列出什么构成对其他各类实体的使用; 这是重要的一个。

朋友声明不是表达。

当函数main()被转换为一个指针并且该指针被分配给p ,这是一个表达式,并且可能被评估(C ++ 03§3.2/ 2):

除非表达式出现在需要整型常量表达式的地方(见5.19), sizeof运算符的操作数(5.3.3),或者是typeid运算符的操作数,并且表达式不指定左值多态类型(5.2.8)。

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

更多推荐

本文发布于:2023-08-07 23:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466055.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   meant   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!