Main Content

CERT C++: DCL59-CPP

Do not define an unnamed namespace in a header file

Description

Rule Definition

Do not define an unnamed namespace in a header file.1

Polyspace Implementation

The rule checker checks for Unnamed namespace in header file.

Examples

expand all

Issue

Unnamed namespace in header file detects an unnamed namespace in a header file, which can lead to multiple definitions of objects in the namespace.

Risk

According to the C++ standard, names in an unnamed namespace, for instance, aVar here:

namespace {
   int aVar;
}
have internal linkage by default. If a header file contains an unnamed namespace, each translation unit #include-ing the header file defines its own instance of objects in the namespace. The multiple definitions are probably not what you intended and can lead to unexpected results, undesired memory usage or inadvertently violating the one-definition rule.

Fix

Specify names for namespaces in header files or avoid using namespaces in header files.

Example – Unexpected Results from Unnamed Namespaces in Header Files

Header File: aHeader.h

namespace { //Noncompliant
   int aVar;
}

First source file: aSource.cpp

#include "aHeader.h"
#include <iostream>

void setVar(int arg) {
    std::cout << "Current value: " << aVar << std::endl;
    aVar = arg;
    std::cout << "Value set at: " << aVar << std::endl;
}

Second source file: anotherSource.cpp

#include "aHeader.h"
#include <iostream>

extern void setVar(int);

void resetVar() {
    std::cout << "Current value: " << aVar << std::endl;
    aVar = 0;
    std::cout << "Value set at: 0" << std::endl;
}

void main() {
    setVar(1);
    resetVar();
}

In this example, the unnamed namespace leads to two definitions of aVar in the translation unit from aSource.cpp and the translation unit from anotherSource.cpp. The two definitions lead to the possibly unexpected output:

Current value: 0
Value set at: 1
Current value: 0
Value set at: 0

Correction – Avoid the Unnamed Namespace

One possible correction is to simply avoid a namespace in the header file.

Header File: aHeader.h

extern int aVar;

First source file: aSource.cpp

#include "aHeader.h"
#include <iostream>

void setVar(int arg) {
    std::cout << "Current value: " << aVar << std::endl;
    aVar = arg;
    std::cout << "Value set at: " << aVar << std::endl;
}

Second source file: anotherSource.cpp

#include "aHeader.h"
#include <iostream>

extern void setVar(int);
int aVar;

void resetVar() {
    std::cout << "Current value: " << aVar << std::endl;
    aVar = 0;
    std::cout << "Value set at: 0" << std::endl;
}

void main() {
    setVar(1);
    resetVar();
}

You now see the expected sequence in the output:

Current value: 0
Value set at: 1
Current value: 1
Value set at: 0

Check Information

Group: 01. Declarations and Initialization (DCL)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.