NewBie003
NewBie003

Reputation: 79

MSVC rejects delegating constructor using base class alias, adopted from the base class

The following code compiles with g++ 14.2 and clang++ 18.1, but fails with MSVC 17.10.

class Base {
public:
  using peak = Base;
  Base() = default;
};

class Derived : public Base {
protected:
  using Base::peak; 

public:
  Derived() : peak() { } // rejected by MSVC
};

int main() {
  Derived d1{};
}

MSVC errors:

error C2437: 'peak': has already been initialized
error C2437: 'peak': has already been initialized

Is the use of the peak alias valid C++?

Note: If we use using peak = Base::peak instead of using Base::peak, compilation succeeds with all three compilers.

Upvotes: 7

Views: 230

Answers (1)

anon
anon

Reputation:

The program is well-formed.This can be seen from class.base.init that explicitly allows this:

A mem-initializer-list can initialize a base class using any class-or-decltype that denotes that base class type.

[Example 1: 
struct A { A(); };
typedef A global_A;  <---------------------
struct B { };                             |
struct C: public A, public B { C(); };    |
                                          v
C::C(): global_A() { }                 // mem-initializer for base A
— end example]

Now class-or-decltype includes nested-name-specifieropt type-name where the optional nested-name-specifier_opt will be empty in our case and type-name includes typedef-name(which is peak) in our case.


Here is the newly submitted msvc bug:

MSVC rejects valid program involving using declaration for alias used in mem-initializer-list

Upvotes: 7

Related Questions