How can a export an existing concept from a c++20 module

Hey guys.
I have been using concepts for some time and they work very well. Now I'd like to add module support.
The important part here: I need to be backwards compatible (i.e. use classic includes and imports side by side in different build configurations)

I'm using latest MSVC with latest CMake.

One of my concepts looks like this

ITask.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#ifdef USE_CXX_MODULES
import std.compat;
#else
#include <concepts>
#endif //USE_CXX_MODULES

namespace ns
{
  template < typename T >
  concept ITask_T = requires(T a)
  {
    { a.start() }->std::same_as<void>;  ///< Starts the task
    { a.stop() } -> std::same_as<void>; ///< Stops the task
  };
}


I can use this concept to pass ITask_T instead of a generic parameter to my templated classes.

One (simple) implementation could be

MyTask.hpp
1
2
3
4
5
6
7
8
9
10
#pragma once
namespace ns
{
  class MyTask
  {
    public:
    void start();
    void stop();
  };
}


My module looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module;

#include <ITask.hpp>
#include <MyTask.hpp>

export module my_module;

export
{
  namespace ns
  {
    using ::ns::MyTask;
  }
}


However i haven't found a way to export my ITask conept as well.
If I mix include <ITask.hpp> and import my_module I run into name clashses for ITask.

I cannot move the concept into the module since I need to preserve backward compatibility. Does somebody know how to export concepts form modules?
Thx guys :)
Last edited on
If'n I understand what you are trying to do it would require a very deep dive into the guts of your library. Something akin to what might be required to modularize Boost.

https://anarthal.github.io/cppblog/modules

Personally I would maintain two separate codebase implementations for whatever I'm doing. One that uses regular headers/translations units and a 2nd one for modularization. The amount of work needed to maintain a single codebase that is both modular/non-modular is likely not worth the effort.

At least not for me.

Registered users can post here. Sign in or register to post.