I have a function called testin namespace buzz.
From this test function i am calling another function called dummy which is inside namespace example.
I get the following error:
Dummy is not a member of example.
Can you please tell me how to communicate between 2 different namespaces?
Thanks
Following code works with gcc (as expected). Your problem must be with something that is not in the question.
#include <iostream>
namespace example
{
void dummy() { std::cout << "Dummy\n"; }
}
namespace buzz
{
void test() { example::dummy(); }
}
int main()
{
buzz::test();
}
If the namespace is not nested, you should start navigating from the root one, i.e.:
Instead of:
example::dummy
Write:
::example::dummy
You need to provide code for this query. Otherwise just from your question, I guess you are making spelling error:
namespace example {
void dummy() {}
}
namespace buzz {
void test () { example::Dummy(); } // capital 'D' instead of 'd' for dummy
}
Naturally, Dummy is not a member of example. :))
Related
I have to define two functions, say, foo() and bar() in the same namespace and the same file. For the definition of the first, foo(), I want to use all symbols of, say, namespace other, but don't want symbols from namespace other to be automatically in scope for my other function, bar(). Is this possible? How?
(note: I don't want to know about alternative "solutions" either avoiding this problem of mitigating it, such as namespace o=other etc.)
Yes, it is possible:
void foo()
{
using namespace abc;
....
}
or
void foo()
{
using abc::x;
using abc::y;
using abc::z;
....
}
#include <iostream>
void quux() { std::cout << "root\n"; }
namespace other {
void quux(int x = 0) { std::cout << "other\n"; }
}
namespace taxes {
void foo() {
using namespace other;
quux(3);
};
void bar() {
quux();
}
}
int main() {
taxes::foo();
taxes::bar();
}
Live example
Note that quux in bar would be ambiguous if it could see other::quux, but it cannot.
On the other hand, this does not give you access to namespace other in the 'head' of the function foo (the parameters etc), but that is a rare requirement. There might be a solution involving inline namespaces or the like, but probably not worth the confusion.
I was wondering, how I could set a const member variable of an unnamed namespace as a default function parameter where the function is declared in an named namespace.
Well I guess this is hard to explain for me, here is an example of what I want to do:
//foo.h
namespace foo
{
void justAFunction(std::string function_string = unnamed_str);
}
//foo.cpp
#include "foo.h"
namespace foo
{
namespace
{
const std::string unnamed_str = "simple string";
}
void justAFunction(std::string function_string)
{
...
}
}
This does not link...
I could still write the default parameter in the function definition, but that is not what I want, since the caller won't see it, right?
Any advice how to code this correct?
Assume I have the method defined in the two different namespaces:
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
//do work...
}
}
The foo::print() and the bar::print() functions are absolutely equal. My project uses the numerous calls of these functions.
Is there a way to remove one of the print() definitions without changing the calls of these function? I mean something like the following (of course, C++ language doesn't allow this construction, it's just an example):
namespace foo, bar //wrong code!
{
void print()
{
//do work...
}
}
If there is no way to refactor the code as I want, please tell me, do you like the following decision? Will you be glad if your project contains such code? :)
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
foo::print();
}
}
ADD:
Thank you guys, I'm fully satisfied by your answers. Just one moment I want you to clarify: is there a difference between using ::foo::print and using foo::print?
You can achieve this with a using declaration.
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
using foo::print;
}
EDIT
Regarding the difference between ::foo::print and foo::print: prepending a qualified name with :: means that you explicitly refer to the one in the global namespace. This can be used to select the global one, even if there is another item with the same name closer in scope.
How about using declaration:
namespace foo
{
void print()
{
// do work ...
}
}
namespace bar
{
using ::foo::print;
}
Using ::foo::print instead of foo::print is an important point. If you would have another foo inside of bar:
namespace foo
{
void print()
{
// 1 ...
}
}
namespace bar
{
namespace foo
{
void print()
{
// 2 ...
}
}
using foo::print; // refers to 2
using ::foo::print; // refers to 1
}
then you'd see the merit of using ::. To summarize, prepending :: is a safe way to ensure that another nested namespace foo, which could be potentially added in future, will not bring you any surprises.
With a using declaration:
namespace bar
{
using ::foo::print;
}
Probably the best solution is this:
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
foo::print();
}
}
However you can write:
namespace foo
{
void print()
{
//do work...
}
}
namespace bar
{
void print()
{
//do work...
}
}
This is not recomended, but there is no evil. Compilers are smart enough to understand that these two functions are the same and can be used the same assembly code.
Remember, namespace keyword is for programmers, compiler doesn't care about them very much, for compiler namespaces are like prefix for functions/methods/classes.
Sample
namespace Q {
namespace V {
void f(); // enclosing namespaces are the global namespace, Q, and Q::V
class C { void m(); };
}
void V::f() { // enclosing namespaces are the global namespace, Q, and Q::V
extern void h(); // ... so this declares Q::V::h
}
void V::C::m() { // enclosing namespaces are the global namespace, Q, and Q::V
}
}
that this snippet of code actually do?
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
void test();
namespace {
static struct StaticStruct {
StaticStruct() {
test();
}
} TheStaticSupport;
}
int main(void) {
return 0;
}
void test() {
printf("testing function\n");
}
why does the test function actually get called? and why use the "anonymous" namespace? I found this piece of code in an open source project...
This:
static struct StaticStruct {
StaticStruct() {
test();
}
} TheStaticSupport;
Is equivalent to this:
struct StaticStruct {
StaticStruct() {
test();
}
};
static StaticStruct TheStaticSupport;
It defines a type named StaticStruct and an instance of the type named TheStaticSupport with internal linkage (though, since it is declared in an unnamed namespace, the static is redundant).
The constructor for TheStaticSupport is called before main() is entered, to construct the object. This calls the test() function.
The anonymous namespace gives the contained objects internal linkage, as their fully qualified name can never be known to anyone outside the translation unit. It's the sophisticated man's version of the old static in C.
Note that you do declare a global object of type StaticStruct, and its constructor (which runs before main() is called) calls test().
Consider the following snippet:
void Foo() // 1
{
}
namespace
{
void Foo() // 2
{
}
}
int main()
{
Foo(); // Ambiguous.
::Foo(); // Calls the Foo in the global namespace (Foo #1).
// I'm trying to call the `Foo` that's defined in the anonymous namespace (Foo #2).
}
How can I refer to something inside an anonymous namespace in this case?
You can't. The standard contains the following section (§7.3.1.1, C++03):
An unnamed-namespace-definition behaves as if it were replaced by
namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }
where all occurrences of unique in a
translation unit are replaced by the
same identifier and this identifier
differs from all other identifiers in the entire program.
Thus you have no way to refer to that unique name.
You could however technically use something like the following instead:
int i;
namespace helper {
namespace {
int i;
int j;
}
}
using namespace helper;
void f() {
j++; // works
i++; // still ambigous
::i++; // access to global namespace
helper::i++; // access to unnamed namespace
}
While Georg gives standard-complient, correct, right, and respectable answer, I'd like to offer my hacky one - use another namespace within the anonymous namespace:
#include <iostream>
using namespace std;
namespace
{
namespace inner
{
int cout = 42;
}
}
int main()
{
cout << inner::cout << endl;
return 0;
}
The only solution I can think of that doesn't modify the existing namespace arrangement is to delegate main to a function in the anonymous namespace. (main itself is required to be a global function (§3.6.1/1), so it cannot be in an anonymous namespace.)
void Foo() // 1
{
}
namespace
{
void Foo() // 2
{
}
}
namespace { // re-open same anonymous namespace
int do_main()
{
Foo(); // Calls local, anonymous namespace (Foo #2).
::Foo(); // Calls the Foo in the global namespace (Foo #1).
return 0; // return not optional
}
}
int main() {
return do_main();
}
The only real way is to put the code you want to access that namespace within the namespace itself. There's no way to resolve to the unnamed namespace otherwise, since it has no identifier you can give it to solve the ambiguous resolution problem.
If your code is inside the namespace{} block itself, the local name gets priority over the global one, so a Foo() will call the Foo() within your namespace, and a ::Foo() will call the namespace at global scope.
Just rename the local namespace function.