Date: Thu, 22 May 2025 12:50:53 +0200
I wonder what the expectations are when a constructor has an attribute
(like [[deprecated]]) and is inherited by a derived class---should that
attribute also be honored by the (implicit) inheriting constructor? For
example, given
> struct B {
> [[deprecated]] B();
> [[deprecated]] B(int);
> [[deprecated]] void f();
> };
> struct D: B {
> using B::B;
> D(bool);
> using B::f;
> void f(bool);
> };
> void f() { // Clang GCC MSVC
> B b1; // + + +
> B b2{}; // + + +
> B b3(0); // + + +
> B b4(0); // + + +
> D d1; // (+) (+) (-)
> D d2{}; // - - (-)
> D d3(0); // - (+) -
> D d4(0); // - - -
> D d5(false);
> b1.f(); // + + +
> b1.f(); // + + +
> d1.f(); // + + +
> d1.f(); // + + +
> d1.f(false);
> }
various compilers behave differently (Clang 20.1.0, GCC 15.1, MSVC
17.13, see <https://gcc.godbolt.org/z/Pbffd3nzP>): While I would
naively assume that all the lines with a trailing comment would be
warned about, that appears to hardly be the case for the uses of
inherited constructors (d1--d4). Clang and GCC somewhat indirectly
("<source>:17:7: note: in implicit default constructor for 'D' first
required here", etc.) warn about only some uses (but do warn about all
the individual calls to member function f, as would meet my
expectations). (And MSVC even refuses to compile the declarations d1
and d2, "'D': no appropriate default constructor available".)
Thoughts?
(like [[deprecated]]) and is inherited by a derived class---should that
attribute also be honored by the (implicit) inheriting constructor? For
example, given
> struct B {
> [[deprecated]] B();
> [[deprecated]] B(int);
> [[deprecated]] void f();
> };
> struct D: B {
> using B::B;
> D(bool);
> using B::f;
> void f(bool);
> };
> void f() { // Clang GCC MSVC
> B b1; // + + +
> B b2{}; // + + +
> B b3(0); // + + +
> B b4(0); // + + +
> D d1; // (+) (+) (-)
> D d2{}; // - - (-)
> D d3(0); // - (+) -
> D d4(0); // - - -
> D d5(false);
> b1.f(); // + + +
> b1.f(); // + + +
> d1.f(); // + + +
> d1.f(); // + + +
> d1.f(false);
> }
various compilers behave differently (Clang 20.1.0, GCC 15.1, MSVC
17.13, see <https://gcc.godbolt.org/z/Pbffd3nzP>): While I would
naively assume that all the lines with a trailing comment would be
warned about, that appears to hardly be the case for the uses of
inherited constructors (d1--d4). Clang and GCC somewhat indirectly
("<source>:17:7: note: in implicit default constructor for 'D' first
required here", etc.) warn about only some uses (but do warn about all
the individual calls to member function f, as would meet my
expectations). (And MSVC even refuses to compile the declarations d1
and d2, "'D': no appropriate default constructor available".)
Thoughts?
Received on 2025-05-22 10:50:59