News for hackers. Stuff that matters.
Re:C++ Modules Might Be Dead-on-Arrival (2019) by dgellow on Friday December 31, 2021 @02:14AM (#29745749)
Edit: I just notice now that you said "TU", my mind autocompleted your sentence with "file" instead the first time I read it. Still keeping my comment here, module :private is not well known and a nice way to declare a distinct interface Translation Unit while keeping everything in one file.

You can do this, but you can also use the "module :private" fragment if you want to have everything in one file while still keeping the distinction between interface and implementation.

Example:

  // File "mymodule.ixx"
  module;

  #include <string>

  // declare interface
  export module mymodule;

  export namespace mymodule {
      // templates have to be declared
      // in interface to be known at
      // compile-time
      template<typename T>
      T add(T x, T y) {
          return x + y;
      }

      std::string greetings();
  }

  // implement interface
  module :private;

  std::string mymodule::greetings() {
      return "hello";
  }

There are no comments on this comment yet.