C ++中main的签名参数可以具有unsigned和const限定词吗? [英] Can the arguments of main's signature in C++ have the unsigned and const qualifiers?

查看:115
本文介绍了C ++中main的签名参数可以具有unsigned和const限定词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该标准明确声明main具有两个有效(即保证有效)签名;即:

The standard explicitly states that main has two valid (i.e., guaranteed to work) signatures; namely:

int main();
int main(int, char*[]);

我的问题很简单,类似以下内容是否合法?

My question is simple, would something like the following be legal?

int main(const unsigned int, const char* const* argv);

我的测试说是",但是我不确定答案,因为我没有通过将int更改为unsigned int以及argv的非顶级const来使main超载吗? ?如果我是,那显然是禁止的.

My tests say 'yes', but I'm unsure of the answer because am I not overloading main by changing int to unsigned int as well as the non top-level const-ness of argv? If I am, then that's clearly prohibited.

那么,这些修改是否可以保证在符合标准的编译器上起作用?

So, are these modifications guaranteed to work on a standards conforming compiler?

推荐答案

C ++ 98标准在3.6.1节第2段中表示

The C++98 standard says in section 3.6.1 paragraph 2

实现不应预定义主要功能.此功能不得重载.它的返回类型应该为int,否则它的类型是实现定义的.所有实现均应允许以下两个main定义:int main()int main(int argc, char* argv[])

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both the following definitions of main: int main() and int main(int argc, char* argv[])

因此,标准没有强制要求环境接受main是可以接受的,但是它是允许的.

So it's not mandated by the standard that the env accepting main is acceptable but it is permissible.

由于经常提到此问题,因此上一段免除独立环境的任何记载,但不记录其行为:

Because this is referred to often, here is the previous paragraph exempting freestanding environments from anything but documenting their behavior:

程序应包含一个称为main的全局函数,这是程序的指定启动位置.它是 实施定义 是否需要在独立环境中定义主程序的程序 功能. [注意:在独立环境中,启动 终止是实现定义的; 启动 包含具有静态存储持续时间的名称空间范围对象的构造函数的执行;终止 包含具有静态存储持续时间的对象的析构函数的执行. ]

A program shall contain a global function called main, which is the designated start of the program. It is implementation defined whether a program in a freestanding environment is required to define a main function. [Note: in a freestanding environment, startup and termination is implementation defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. ]

这篇关于C ++中main的签名参数可以具有unsigned和const限定词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆