4

What's the expected behavior of the following code?
With GCC the output is 0, whereas with clang it's 1.
Which one is correct?

#include <iostream>

static const bool ne = false;

struct a
{
        a() noexcept(ne) {}
        static const bool ne = true;
};

int main()
{
        std::cout << noexcept(a()) << std::endl;
}
sigsegv
  • 73
  • 3

1 Answers1

4

They're both right! It's just ill-formed code. From [basic.class.scope]:

A name N used in a class S shall refer to the same declaration in its context and when re-evaluated in the completed scope of S. No diagnostic is required for a violation of this rule.

This section also includes the example:

[Example:

typedef int c;
enum { i = 1 };

class X {
    char v[i];      // error: i refers to ::i
                    // but when reevaluated is X::i
    int f() { return sizeof(c); } // OK: X::c
    char c;
    enum { i = 2 };
};
[...]  

-end example ]

Without the global scope ne, the code is valid - but gcc fails to compile it because of bug 70142 (unconfirmed still).

Barry
  • 286,269
  • 29
  • 621
  • 977
  • The only compiler that runs the standard example is Clang (tried a few), and clang says the size of v is 1 (and if I understood correctly it should be 2 after it has been re evaluated). The rest of the compilers won't compile due to redefinition of i. – Jts Mar 11 '16 at 14:37
  • @José The code is ill-formed. There's no "correct" value. – Barry Mar 11 '16 at 14:42