Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting class imported from another module

I am experimenting with C++ modules, using clang 5.0, and I am trying to understand how can I export from one module something that I have imported from another module. Is that even possible?

For example, I'd like to have something like this:

// root.hehe.cppm
export module root.hehe;

class hehe
{    
};

and this:

// root.cppm
export module root;

import root.hehe;

export class hehe; // ... doesn't work!
export hehe; // Also doesn't work!
export import root.hehe; // No dice!

So that in the end I can do something like

import root;

// ...

hehe myhehe;

Is such a thing possible? I also tried figuring out if there could be a way to import all the submodules of root, like import root.*, but that didn't work either.

like image 903
Matteo Monti Avatar asked May 04 '26 00:05

Matteo Monti


1 Answers

In C++20 (not whatever prototype version in Clang), you can use either of

export using ::hehe;
export using hehe=hehe;

to do this, with two caveats:

  1. The first form must always use a qualified name (because the syntax was introduced long ago to copy names between namespaces).
  2. You must first be able to use the name you imported, which is not the case in your example because root.hehe did not export it. (For the type alias approach, it’s sufficient to be able to name it via decltype or so.)

You can also use export import root.hehe; to reexport everything exported by the module being imported. There is no wildcard import syntax: module names with dots have no semantics whatsoever (in C++20).

like image 69
Davis Herring Avatar answered May 08 '26 11:05

Davis Herring



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!