Sobes.tech
Junior

What is the analogy of static visibility level in a C++ file?

sobes.tech AI

Answer from AI

The analogy of static visibility level in a C++ file is a namespace declared inside an anonymous (unnamed) namespace.

Before C++11, the static keyword was used to restrict visibility at the file level for functions and static global variables. In modern C++, it is recommended to use unnamed namespaces.

Example:

// In this file, func_in_file and global_var_in_file
// are only visible within this file.
namespace {
    void func_in_file() {
        // ...
    }

    int global_var_in_file = 0;
}

void visible_globally() {
    // func_in_file and global_var_in_file are accessible within this file
    func_in_file();
    global_var_in_file = 1;
}