Consider the following module consisting of three units:
Module implementation unit partition:
module my_module:base;
class Base {
public:
Base() = default;
};
Module interface unit partition:
export module my_module:derived;
import :base;
export class Derived final : public Base {
public:
Derived() = default;
};
Primary module interface unit:
export module my_module;
export import :derived;
The problem is that I cannot instantiate Derived from outside the module as I get the following error:
error: definition of 'Base' must be imported from module 'my_module' before it is required
7 | Derived() = default;
| ^
note: in defaulted default constructor for 'Derived' first required here
118 | Derived d_1;
However, once I define the default constructor for Derived explicitly, like this:
export class Derived final : public Base {
public:
Derived() : Base {} {}
};
the problem is gone. I use Clang 19, and to me this looks like a bug, is it?
It’s a compiler bug, albeit an understandable one: one of the benefits of modules is that you’re not supposed to need to generate the same code in every client, but here the implementation can’t generate a special member function defaulted on its first declaration if it’s not used, so it has to wait.
That doesn’t mean it should fail, though. In particular, module linkage doesn’t make something unusable outside the module: it just means you can’t use its name to find it (and instead can use that name for something else). Various tricks like decltype can be used to refer to such “hidden” entities, and you could even use that to perform the first usage of such a special member function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With