4

Paragraph 7.3.3. of C++2003 standard states that

Using declaration for a class member shall be a member declaration.

This means that the following gives a syntax error:

struct S1
{
    static int var1;
};

using S1::var1;

While the following compiles fine:

namespace N2
{
    int var2;
}

using N2::var2;

Does anybody know the rationale (if any) behind that?

Even more, the standard gives explicit example with static data member of the struct and tells that it should cause syntax error. MS C++ gives this error:

cpptest1.cxx(9) : error C2885: 'S1::var1': not a valid using-declaration at non-class scope

It is still not clear why this should be banned.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • 3
    Other than _a struct is no namespace_? – K-ballo Jun 20 '12 at 18:51
  • What is the message of the syntax error? – kol Jun 20 '12 at 19:14
  • And what? How is this is related to the question? – Kirill Kobelev Jun 20 '12 at 19:15
  • @K-ballo Anyway, a struct is a class (well, sort of), and a class is a namespace on steroids (just see the logic of Stroustrup's book). – kol Jun 20 '12 at 19:17
  • possible duplicate of [using declaration with enum?](http://stackoverflow.com/questions/438192/using-declaration-with-enum) –  Jun 20 '12 at 19:19
  • A class is no namespace either... Its a new name scope, just as structs, but not a namespace. – K-ballo Jun 20 '12 at 19:21
  • I am not so sure that the other question's accepted answer is correct. Note that you *can* have *using S::x* in a derived class' definition. By the logic presented in that answer that should've been illegal. Or, am I missing something? – dirkgently Jun 20 '12 at 19:25
  • The explanation of syntax error C2885 in MSDN: http://msdn.microsoft.com/en-us/library/0swkcwwk(v=vs.80).aspx – kol Jun 20 '12 at 19:37

1 Answers1

0

The reason it doesn't work is simply because the Standard forbids it. Pay special attention to s in the example:

enter image description here

As a workaround, you can say:

auto& var = S1::var;

Making the Standard (and therefore all compilers) more complicated to handle an unusual case that has a convenient workaround just doesn't pass the cost-benefit test.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • The question was not about if it works or not. This is crystal clear. Please, read the initial post carefully. Question was about reasons behind the standard. What you are saying in your last sentence does not qualify for a reason. Text for C2885 says that Microsoft issues this error to comply with the standard. This allows to expect that in earlier versions there were not having this restriction. And this is not surprising. Removing this restriction makes matters simpler. – Kirill Kobelev Jun 21 '12 at 03:52
  • @Kirill: Requiring one compiler developer (Microsoft) to remove support for something they did without the committee's blessing is far cheaper than requiring hundreds of other compilers to add support. – Ben Voigt Jun 21 '12 at 04:52
  • Besides the handful compilers, I am not sure their count is more than 50, there are millions of developers. Every simplification in the definition of the language reduces the time to learn and confusion. Multiply this by the number of C++ programmers. This is the REAL benefit. – Kirill Kobelev Jul 02 '12 at 10:03