Topic: namespace's strange behavior


Author: stephen.clamage_nospam@eng.sun.com (Steve Clamage)
Date: 1997/09/24
Raw View
On 23 Sep 97 04:14:20 GMT, "TytySoft" <amoga@aspdeveloper.net> wrote:

>i wonder if there's a reason to use code like this:
>
>namespace whatever
>{
>    // some code here...
>    using namespace whatever;
>    // more code here...
>}
>
>i couldn't have imagined any situation which require such block of code;

Nor can I. The using-declaration makes names from the namespace
available in the current scope without using qualifiers. Since you are
aready in the scope of that namespace, it shouldn't have any effect.
The oddity you reported in the original article sounds like a compiler
bug to me.



Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "TytySoft" <amoga@aspdeveloper.net>
Date: 1997/09/23
Raw View
hi,

i wonder if there's a reason to use code like this:

namespace whatever
{
    // some code here...
    use namespace whatever;
    // more code here...
}

i couldn't have imagined any situation which require such block of code;
that's till i got in the following situation:

// --- bad code: file "test.cpp" ---

#include <vector>
using namespace std;

namespace n_test
{
struct MyStruct
{
  int a;
  int b;
};

typedef vector<MyStruct> MYVECTOR;

void f(void)
{
  MYVECTOR myVector;
}
}

// --- end of code: file "test.cpp" ---

trying to compile (with MSVC++ 5.0) the code above will generate some nasty
errors:

C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2065: 'MyStruct'
: undeclared identifier
C:\Program Files\DevStudio\VC\INCLUDE\vector(103) : error C2440: 'default
argument' : cannot convert from 'int' to 'const struct n_test::MyStruct &'
                                                                 Reason:
cannot convert from 'int' to 'const struct n_test::MyStruct'
                                                                 No
constructor could take the source type, or constructor overload resolution
was ambiguous


what's interesting is the fact that the problem will go away if i place an
using directive right before the typedef declaration; just like in the
following code:

// --- code OK: file  "test2.cpp" ---

#include <vector>
using namespace std;

namespace n_test
{
struct MyStruct
{
  int a;
  int b;
};

using namespace n_test;
typedef vector<MyStruct> MYVECTOR;

void f(void)
{
  MYVECTOR myVector;
}
}

// --- end of code: file "test2.cpp" ---

is this the supposed to be the normal behavior? i don't think so... i was
thinking that any declaration inside a namespace is _visible_ anywhere in
that specified namespace, though i do not need to use an using directive
like the one above; is there a problem with the compiler or is something
wrong with my understanding?

may someone explain me how this could be possible?

any help will be greatly appreciated,
AM
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]