2

I was testing the name lookup rules for C++. I've got a simple program having 3 files:

$cat testns01.h
struct S{
    static int f(){return 1;}
};

$cat testns02.h
namespace S{
    static int f(){return 2;}
}

$cat testns3.cpp
#include "testns02.h" // namespace
#include "testns01.h" // struct
#include<stdio.h>
int main()
{
    int i = S::f();
    printf("%d\n",i);
    return 0;
}

If I compile and run, I get:

$g++ testns3.cpp && ./a.out
1

OK, I have 3 questions:

  1. name "S" duplication between class name and namespace doesn't conflict?
  2. When both has name "S", seems "struct S" has higher priority

If I comment the line of #include "testns01.h", the program will print 2, still OK. So my third question is:

  1. Does the c++ standard talk about how name lookup resolves duplicated name duplication?
Hurricane Development
  • 2,449
  • 1
  • 19
  • 40
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • Related: http://stackoverflow.com/questions/4070915/classes-and-namespaces-sharing-the-same-name-in-c – Barmar Feb 20 '17 at 11:21
  • @Barmar The difference is, in the link you pointed out, the same name for struct/class and namespae were not defined in the same (global) namespace. I'm surprised that the compiler doesn't even warn about. – Scheff's Cat Feb 20 '17 at 11:28
  • 1
    Version of compiler? I can't reproduce it on g++-4.6, g++-6.1. – ForEveR Feb 20 '17 at 11:43
  • GCC 6.3.0: `error: 'struct S' redeclared as different kind of symbol. previous declaration 'namespace S { }'` – xinaiz Feb 20 '17 at 11:43
  • "If I compile and run, I get:" ... what do you get? – 463035818_is_not_an_ai Feb 20 '17 at 11:53
  • Possible duplicate of [Classes and namespaces sharing the same name in C++](http://stackoverflow.com/questions/4070915/classes-and-namespaces-sharing-the-same-name-in-c) – jww May 05 '17 at 18:12

1 Answers1

2
  1. name "S" duplication between class name and namespace doesn't conflict?

They do.

  1. When both has name "S", seems "struct S" has higher priority

It doesn't. (look below)

  1. Does c++ stardard talke about how name lookup resolves duplicated name duplication?

Yes. Quoting the relevant part of N4140:

§3.3.1 [basic.scope.declarative] / 4

Given a set of declarations in a single declarative region, each of which specifies the same unqualified name,

  • they shall all refer to the same entity, or all refer to functions and function templates; or
  • exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden. [ Note: A namespace name or a class template name must be unique in its declarative region. —end note ]

I think you accidentally got your example working for you, because you repeated the include guards. I was able to reproduce the "preference" for class S from the question:

#ifndef FOO
#define FOO
struct S{
    static int f(){return 1;}
};
#endif

#ifndef FOO
#define FOO
namespace S{
    static int f(){return 2;}
}
#endif

#include<stdio.h>
int main()
{
    int i = S::f();
    printf("%d\n",i);
    return 0;
}

link

krzaq
  • 16,240
  • 4
  • 46
  • 61
  • 1
    Thanks, I found it's a bug of low version of gcc, while higher version of gcc reports this bug. – Troskyvs Feb 20 '17 at 12:54
  • Ah, so my guess wasn't correct. It must be a really old version of gcc, though. 4.4.7 on wandbox still errored on this. – krzaq Feb 20 '17 at 13:02