Unexpected side effects of using inside a namespace
Aug 5, 2016 at 12:41am UTC
Hi,
I just came across the most unexpected situation and I want to share it with all of you just in case you haven't faced it yet.
I have the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13
namespace X
{
using namespace std;
/////
}
void someFunc()
{
using namespace std::chrono;
chrono::hours hrs;
}
I get the following at compilation: Use of undeclared identifier 'chrono', did you mean X::chrono?
Wow! The using directive inside of X seemed to me be to local - never anticipated it would have consequences in following code. I now realize that all symbols U inside std become available as X::U (in other words std::U becomes synonym with X::U).
Wow! Dangerous and unexpected.
Anybody knew about this?
Regards,
Juan Dent
Last edited on Aug 5, 2016 at 12:42am UTC
Aug 5, 2016 at 1:39am UTC
Did you #include the chrono header file?
Aug 5, 2016 at 1:56am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <chrono>
namespace X
{
using namespace std ;
}
void foo()
{
using namespace std::chrono ;
X::chrono::hours a(1) ; // look up 'chrono::hours' in namespace ::X => look up in namespace ::std
std::chrono::hours b(2) ; // look up 'std::chrono::hours' in namespace ::
hours c(3) ; // look up 'hours' in namespace ::std::chrono (using namespace std::chrono)
}
Aug 5, 2016 at 2:25pm UTC
Yes I had included chrono.
Yes JLBorges, you provide the rationale: X::chrono::hours implies lookup 'chrono::hours' in namespace ::X ==> look up in namespace ::std.
Interesting, yet a bit unexpected.
Thanks
Juan
Topic archived. No new replies allowed.