C++20 Module multiple-layer of inheritance

Viewed 227

I found that in Visual Studio 2019 v16.8, the class invoking multiple-layer of inheritance cannot find its deep base class unless import the related module explicitly.

intrfce.ixx file:

export module intrfce;

export class Interface {
public:
    virtual void Fun() = 0;
};

imp_a.ixx file:

export module imp_a;

import intrfce;

export class ImpA : public Interface {
public:
    void Fun() override {}
};

imp_b.ixx file:

export module imp_b;

import imp_a;

export class ImpB : public ImpA {
public:
    void Fun() override {}
};

When building these files, the IDE shows there is an error in imp_b.ixx:

imp_b.ixx(5,33): error C2230: could not find module 'intrfce'

If I import the intrfce explicitly in imp_b.ixx, it will succeed.

export module imp_b;

import intrfce;
import imp_a;

export class ImpB : public ImpA {
public:
    void Fun() override {}
};

But it does not make sense that a class needs to explicitly import all its deep base classes.

Is it a bug or a new standard rule of C++20?

Thank you

1 Answers

7. When a module-import-declaration imports a translation unit T, it also imports all translation units imported by exported module-import-declarations in T; such translation units are said to be exported by T.

Emphasis on exported added by me.

So, I think the intention is that imp_a.ixx should be

export module imp_a;

export import intrfce; // re-export this transitive dependency

export class ImpA : public Interface {
public:
    void Fun() override {}
};

Imports between different units of the same module behave differently.

Related