Why can't redefine type names in class in C++?

According to the book C++ Primer section, 7.4.1 Type Names Are Special:

Ordinarily, an inner scope can redefine a name from an outer scope even if that name has already been used in the inner scope. However, in a class, if a member uses a name from an outer scope and that name is a type, then the class may not subsequently redefine that name.

Accordingly, for example:

typedef double Money;
class Account {
    public:
        Money balance() { return bal; }
    private:
        typedef double Money;
        Money bal;
};

int main() {
    typedef double Money;
    Money asset;
    typedef double Money;
    return 0;
}

When you compile the example above, it will complain:

a.cc:6:24: error: declaration of ‘typedef double Account::Money’ [-fpermissive]
         typedef double Money;
                        ^
a.cc:1:16: error: changes meaning of ‘Money’ from ‘typedef double Money’ [-fpermissive]
 typedef double Money;

So why can not we redefine type names in class, but can we in the inner scope?


My compiler version is g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609.
And there is also a note in that section:

Although it is an error to redefine a type name, compilers are not required to diagnose this error. Some compilers will quietly accept such code, even though the program is in error.


Solution 1:

This is not unique to types. [basic.class.scope]/2:

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.

The reason is that name lookup in class scope is a little special. Consider:

using Foo = int;

struct X {
    Foo a;    // ::Foo, i.e., int
    void meow() { 
        Foo b = a; // X::Foo; error: no conversion from int to char*
    }
    using Foo = char*;
};

Name lookup in member function bodies considers all class members, whether declared before or after the member function (otherwise, a member function defined in a class definition wouldn't be able to use a data member declared later in the class). The result is that you get two Foos with different meanings, even though they both lexically precede the class member Foo's declaration. This can easily lead to extremely confusing and brittle code, and so the standard bans it.

Solution 2:

When the compiler reads the line

    Money balance() { return bal; }

in the class definition, it already uses the definition of Money outside the class. That makes the line

    typedef double Money;

inside the class a problem. However, you may use redefine Money inside the class before it is used in the class. The following is OK.

typedef double Money;

class Account {
    public:
        typedef double Money;
        Money balance() { return bal; }
    private:
        Money bal;
};

The key point in the quote is:

hence the class may not subsequently redefine that name.

Solution 3:

I would like to try to answer some questions from your comments.

Comment 1

"But in the function main, we can redefine typedef double Money even if it is defined after the statement Money asset"

So you are asking, why typedef identifier can be defined more than once in a non-member function(in a non-class scope)?

The answer is here:Repeated typedefs - invalid in C but valid in C++?

Comment 2

So, in this example, two Foos with different meaning both lexically precede the statement Foo b = a in function meow. Then the compiler can't determine which the type of b is. Is it correct or not?

The complier can determine the type of b in that code chunk. B's type is obvisouly char* while the type of a is int.

Although two Foo with different meaning both lexically precede the statement Foo b = a in function meow, Foo defined as int precedes Foo defined as char*. The book says that the name lookup process is different:

• First, the member declarations are compiled.
• Function bodies are compiled only after the entire class has been seen.

So in the first step, while compiling member declarations, Foo a and using Foo = char* get compiled in order. And the first Foo uses outside definition of Foo, which is int. Then, an inner scope Foo is created, with type char*. After that, the compiler starts to compile the function body. For function meow, Foo b uses inner scope Foo, which is char*, while for a, which is already get compiled in the first step, is a int Foo. So that's how the conversion error occurs.

Comment 3

I want to know is that why "the class may not subsequently redefine that name." But "an inner scope can redefine a name from an outer scope even if that name has already been used in the inner scope." –

R Sahu's point (and I think it is what the book wants to say) is that if you really want to redefine a typedef identifier, you can only do this in the very beginning of your class. So there won't be any "ambiguity" about that identifier among the context.

Summary:

Allow this:

(This can't compile in g++ (because standard ban this) but can in Visual Studio, because logically there's no conflict here.)

typedef double Money;
class Account {
    public:
        Money balance() { return bal; }
    private:
        typedef double Money;
        Money bal;
};

is very easy to cause things like this:

(can't compile in both g++ and Visual Studio, because logically there is conflict here.)

using Foo = int;

struct X {
    Foo a;    // ::Foo, i.e., int
    void meow() { 
        Foo b = a; // X::Foo; error: no conversion from int to char*
    }
    using Foo = char*;
};

so if you really want to redefine a typedef identifier inside a class. Only do this:

(can compile in both g++ and Visual Studio, because logically there's no conflict here and standard only allows this.)

typedef double Money;

class Account {
    public:
        typedef double Money;  
        //put the redefine statement in the very beginning
        Money balance() { return bal; }
    private:
        Money bal;
};

PS:

Explain the code:

typedef double Money;
class Account {
    public:
        Money balance() { return bal; }
    private:
        typedef double Money;
        Money bal;
};

These code is logically right but for standard it is banned. Same compiling step as mentioned above, first compile function balance's declaration, so Money here is the outside Money. Then complie typedef double Money we get a inner scope Money and the type of bal is Account::Money not the outside one.

So practically you can do this with Visual Studio compiler but not with g++:

typedef double Money;
class Account {
    public:
        Money shit = 12.34; //outside money, type is double
    private:
        typedef string Money;  
        Money bal;   //bal is string not double
};

Thanks for the enlightenment from the other two answers. And there's some prediction in my post which is my personal deduction. If there's anything wrong, feel free to correct it.