Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++20 Module multiple-layer of inheritance

Tags:

c++

c++20

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

like image 283
czs108 Avatar asked Sep 17 '25 02:09

czs108


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.

like image 76
Useless Avatar answered Sep 19 '25 16:09

Useless