This is the mail archive of the
gdb@sources.redhat.com
mailing list for the GDB project.
a bestiary of namespace examples
- From: David Carlton <carlton at math dot stanford dot edu>
- To: gdb <gdb at sources dot redhat dot com>
- Date: 04 Oct 2002 15:05:02 -0700
- Subject: a bestiary of namespace examples
I'm trying to build a bestiary of ways in which name lookup can happen
in C++. It's still very much a work in progress (I haven't done
anything with members of classes or with overloading, for example),
but I wanted to post it here in case any C++ experts have more good
examples to add. (Either of examples that will work or of examples
that you might think would work but that, in fact, won't work.) C++
name lookup, unfortunately, looks complicated enough that I'm not at
all confident that I won't miss weird possibilities if I just go by
what I can get out of the Standard or by what I can get out of various
reference books.
I'm including what I've got so far after my signature; please e-mail
me with any additions you can think of.
David Carlton
carlton@math.stanford.edu
// This file is intended to give you an idea of when names are and
// aren't in scope based on various namespace/using declarations.
// It's intended to compile, so I've commented out statements that
// you might think could work but actually won't.
// Compile with something like g++ -S -g -dA namespace.cc
int g1 = 1;
namespace A
{
int a1 = 1;
int a2 = a1; // = A::a1
// a4's definition hasn't shown up yet.
//int a3 = a4;
}
namespace B
{
int b1 = 1;
}
namespace A
{
int a4;
int a5 = a1; // = A::a1
// namespace B isn't visible.
//int a6 = b1;
int a7 = B::b1;
using namespace B;
int a8 = b1;
}
// namespace B isn't visible.
//int g2 = b1;
namespace A
{
int a9 = b1; // B::b1, from earlier using.
}
// D isn't visible yet.
//int g3 = D::a4;
namespace D = A;
int g4 = D::a4;
namespace A
{
namespace AA
{
int aa1 = a1; // = A::a1
int aa2 = b1; // = B::b1, from earlier using in A.
}
}
namespace A
{
namespace
{
int au1 = 1;
}
namespace
{
// This is the same namespace as the earlier unnamed namespace, so
// we can't redefine au1.
//int au1 = 2;
}
int a10 = au1;
int a11 = D::au1; // = A::<unnamed>::au1
}
namespace B
{
using A::a1;
int b2 = a1;
}
// au1 is visible within A, but not here.
//int g5 = au1;
int g6 = A::au1;
namespace A
{
void af1()
{
a1;
au1;
b2;
}
void af2();
}
void A::af2()
{
a1;
au1;
b2;
}
namespace A
{
class AC {};
}
void gf1(A::AC theAC)
{
// None of these work: namespace A isn't in scope.
//a1;
//au1;
//b2;
}
namespace A
{
int af3(A::AC theAC)
{
return 1;
}
A::AC af4()
{
}
}
// Namespaces get added to lookup list based on parameters to
// functions, but not based on return type.
A::AC g7;
int g8 = af3(g7);
//A::AC g9 = af4();
// This is here just so I can see what compilation info for statics
// looks like.
static int g10 = 1;