Main Content

MISRA C++:2008 Rule 7-3-2

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

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 might defy developer expectations.

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>

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: Declarations
Category: Required

Version History

Introduced in R2013b