Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a namespace::function() declaration useful?

Single File Example

Here is a simple program using namespaces.

#include <iostream>

namespace foo {
    void hello();
}

void foo::hello() {
    std::cout << "hello\n";
}

void foo::hello(); // Why is this syntax allowed? When is it useful?

int main() {
    foo::hello();
}

This program compiles fine and produces the expected output.

$ ./a.out
hello

I want to know when is the void foo::hello(); declaration useful? In this program, clearly this declaration is redundant. But since this syntax exists, this must be useful in some other scenario?

Two-File Example

Here is an example that shows that the void foo::hello(); declaration standing alone is useless.

// foo.cpp
#include <iostream>

namespace foo {
    void hello();
}

void foo::hello() {
    std::cout << "foo::hello\n";
}
// main.cpp
void foo::hello(); // This does not work!

int main()
{
    foo::hello();
}

Trying to compile the above two files leads to the following errors:

$ clang++ foo.cpp main.cpp
main.cpp:1:6: error: use of undeclared identifier 'foo'
void foo::hello();
     ^
main.cpp:5:5: error: use of undeclared identifier 'foo'
    foo::hello();
    ^
2 errors generated.

The only way to fix main.cpp in the above example seems to be include the namespace declaration:

// main.cpp - fixed
namespace foo {
    void hello();
}

void foo::hello(); // But now this is redundant again!

int main()
{
    foo::hello();
}

So after we get this code to compile properly, the void foo::hello(); declaration seems redundant again. Is there any scenario where such a declaration would be playing a useful role?

like image 323
Lone Learner Avatar asked May 17 '18 11:05

Lone Learner


People also ask

What is the purpose of namespace in C++?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is the advantage of using namespace in C++?

Advantages of namespace In one program, namespace can help define different scopes to provide scope to different identifiers declared within them. By using namespace - the same variable names may be reused in a different program.

How do you call a namespace function in C++?

Namespaces in C++ You only need to prefix the function you wish to call with namespace_name:: -- similar to how you would call a static member function of a class. Another convenience of namespaces is that they allow you to use the same function name, when it makes sense to do so, to perform multiple different actions.

When was namespace added C++?

Namespaces were introduced to the C++ Standard in 1995 and usually they are defined like this: A namespace defines a new scope. They provide a way to avoid name collisions. Namespaces in C++ are most often used to avoid naming collisions.


1 Answers

In most cases in C++, for anything that can be either declared without defining it or can be completely defined, a declaration or a definition of that thing can appear in all the same contexts. So this is probably just a way of keeping the pattern consistent.

The C++ Standard does not go out of its way to forbid things that are a logical consequence of its other rules but just not useful, as long as it's reasonably clear what will happen if someone does it anyway. If it did add these restrictions, that would put extra work on compiler writers for no real benefit.

like image 149
aschepler Avatar answered Oct 27 '22 00:10

aschepler