Search code examples
c++compiler-errorsnamespacesusingusing-directives

In what scopes is the "using namespace" clause valid?


I once heard/read (reference has slipped my mind; unable to cite it) that the "using namespace" clause is valid in any scope, but it appears to not be valid in a class scope:

// main.cpp

#include <iostream>

namespace foo
{
  void func() { std::cout << __FUNCTION__ << std::endl; }
};

class Foo
{
using namespace foo;

  public:
    Foo() { func(); }
};

int main( int argc, char* argv[] )
{
  Foo f;
}

.

$ g++ --version
g++ (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

.

$ g++ -g ./main.cpp 
./main.cpp:12:7: error: expected nested-name-specifier before 'namespace'
 using namespace foo;
       ^~~~~~~~~
./main.cpp: In constructor 'Foo::Foo()':
./main.cpp:15:13: error: 'func' was not declared in this scope
     Foo() { func(); }
             ^~~~
./main.cpp:15:13: note: suggested alternative:
./main.cpp:7:8: note:   'foo::func'
   void func() { std::cout << __FUNCTION__ << std::endl; }
        ^~~~
$
$ g++ --std=c++11 -g ./main.cpp 
./main.cpp:12:7: error: expected nested-name-specifier before 'namespace'
 using namespace foo;
       ^~~~~~~~~
./main.cpp: In constructor 'Foo::Foo()':
./main.cpp:15:13: error: 'func' was not declared in this scope
     Foo() { func(); }
             ^~~~
./main.cpp:15:13: note: suggested alternative:
./main.cpp:7:8: note:   'foo::func'
   void func() { std::cout << __FUNCTION__ << std::endl; }
        ^~~~

The following variation of class Foo results in the same compiler error:

class Foo
{
using namespace ::foo;

  public:
    Foo()
    {
      func();
    }
};

The following variations of class Foo result in no compiler errors or warning:

class Foo
{
  public:
    Foo()
    {
      using namespace foo;
      func();
    }
};

.

class Foo
{
  public:
    Foo()
    {
      foo::func();
    }
};

My (incorrect?) understanding of the compiler error, based on reading posts such as this and this, is that error essentially requires full scoping of the used namespace, i.e. what I attempted in the first variation to class Foo, above.

Please note: in addition to explicit use of the --std=c++11 compiler flag, the compiler version used is well above the minimum necessary to not encounter this error (without explicit use of the --std=c++11 compiler flag), according to the noted Stack Overflow Q&A.

What does this compiler error mean (if different than my above-stated understanding) in this context? (My use appears different than that in the two noted Stack Overflow Q&A).

In general: in what scopes is the "using namespace" directive valid?


Solution

  • From cppreference.com:

    Using-directives are allowed only in namespace scope and in block scope.

    So you can use them within a namespace (including the global namespace) or within a code block. A class declaration is neither of those.