Skip to content

C++14: invalid inherited constructors are accepted #53653

Open
@Fedr

Description

@Fedr

This code is probably valid in C++17, but not in C++14:

struct B {
    B(int) {}
    B(int&&) {}
};

int i = 1;
B b(i); //ok everywhere

struct C : B {
    using B::B;
};

C c(i); //ok in Clang only

Clang is the only compiler that currently accepts it. Demo: https://gcc.godbolt.org/z/MbGqGE4Yr

According to C++14 standard class.inhctor#8:

... If that user-written constructor would be ill-formed, the program is ill-formed. Each expression in the expression-list is of the form static_cast<T&&>(p), where p is the name of the corresponding constructor parameter and T is the declared type of p.

So struct C definition must be equivalent to:

struct C : B {
    C(int x) : B(static_cast<int&&>(x)) {}
};

making the program ill-formed due to ambiguity. Demo: https://gcc.godbolt.org/z/hK9q4vPdj

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions