In this guide, we will describe the working and differences between unnamed/anonymous namespaces and static functions in C++.
What are Unnamed/Anonymous Namespaces in C++?
The unnamed/anonymous namespace is an exclusive feature of C++ that allows users to create functions and variables that are only accessible within a certain translation unit. In terms of functionality, any code which uses the same header file, as well as the source file that contains the unnamed/anonymous namespace, has access to the functions and variables contained within it, but code in other translation units cannot show the variables and functions.
Syntax
In C++, the syntax of an unnamed/anonymous namespace is provided below:
//body
}
Example: Implementation of Unnamed/Anonymous namespaces
To use the unnamed/anonymous namespace in C++, check out the provided example:
namespace {
int num = 100;
void print() {
std::cout << "number = " << num << std::endl;
}
}
int main() {
print();
return 0;
}
In this example, we constructed an unnamed/anonymous namespace with the integer variable “num” and “print()” functions to print the value of “num”. For displaying a value of the variable, use the “print()” method within the “main()” function. As follows:
What are Static Functions in C++?
It’s a member function that only allows access to static data members and is unable to use non-static data members or invoke non-static member functions. Even if the class doesn’t contain any objects, it can still be accessed. It is also used to keep a single copy for a class member function throughout all class objects.
Syntax
In C++, the syntax for defining a static function is as follows:
The “return_type” is the function’s return type, “func_name()” is the function’s name that takes arguments. The term static specifies that the function can only be available inside the same file where it is declared.
Example: Implementation of static Function
To implement the static function, check out the provided code:
using namespace std;
class Cell{
private:
static string cell_name;
public:
//static function I
static void name(){
cout << "Mobile: "<< cell_name<<endl;
}
//static function II
static void set_name(string name){
cell_name = name;
}
};
//Initializing private static member
string Cell::cell_name = "";
int main()
{
//accessing static function
Cell::set_name("Apple iphone");
Cell::name();
return 0;
}
In the above-described code, we declared a “Cell” class with two public static member methods named “name()” and “set_name()” that will access a private static string variable named “cell_name”. The “name()” method shows the value of the “cell_name” variable on the console. On the other hand, the “set_name()” function modifies the “cell_name” variable’s value. In the “main()” function, invoked the static “set_name()” method to show the value of the “cell_name” variable. Lastly, call the static “name()” function to display the value of “cell_name” on the console. As shown in the below screenshot:
Note: Overall, both strategies are effective in constraining the scope for functions and variables while preventing name conflicts. You can select one of these based on your requirements.
Conclusion
Unnamed/anonymous namespaces as well as static functions are employed in C++ to limit the scope of functions and variables while preventing naming conflicts. Although unnamed/anonymous namespaces enable variables and functions to be accessible across different files, static functions are only available in the file in which they are declared. This tutorial illustrated the difference between unnamed/anonymous namespaces and static functions in C++.