Main Content

MISRA C++:2023 Rule 6.0.4

The identifier main shall not be used for a function other than the global function main

Since R2024b

Description

Rule Definition

The identifier main shall not be used for a function other than the global function main.

Rationale

Typically, the main function lives in the global namespace and acts as the entry point to a program. The use of main in other contexts is confusing and unexpected.

Polyspace Implementation

The rule checker reports a violation if you use the identifier main in a namespace other than the global namespace.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstdint>

class ExampleA {
	public:
		void main(void) {}    //Noncompliant
};

int32_t main() {                   //Compliant
    //...
    return 0;
}

namespace Backups {
    int32_t main() {               //Noncompliant
        //...
        return 0;
    }
}

The use of main in a namespace other than the global namespace violates the rule.

Check Information

Group: Basic Concepts
Category: Required

Version History

Introduced in R2024b