I have defined a concept to check if a compile time interface is fulfilled.
IMutex.hpp
#pragma once
#include <concepts>
namespace foo
{
template<typename T>
concept IMutex_T = requires (T a) {
{ a.lock() } -> std::same_as<void>;
{ a.try_lock() } -> std::same_as<bool>;
{ a.unlock() } -> std::same_as<void>;
};
}
In my class templates I can now use IMutex_T
instead of a generic type.
One (very primitive) implementation of this interface would be
stdMutex.hpp
#pragma once
#include <mutex>
namespace foo
{
using stdMutex = std::recursive_mutex;
}
My modules look like this
// Global module fragment where #includes can happen
module;
#include <IMutex.hpp>
#include <stdMutex.hpp>
// first thing after the Global module fragment must be a module command
export module my_module;
export
{
namespace foo
{
using ::foo::stdMutex;
}
}
module: private;
// possible to include headers here, but they will not be exported to the client
I haven’t found a way to add the concept here. using ::foo::IMutex_T
does not seem to work.
The only thing which did work is copying the concept together with an export concept
directly into the module (after the export module my_module;
).
However, I’d like to preserve backward compatibility (i.e., use classic includes) so I cannot move the concept into the module. Is there any way to export this concept from the module? (Currently I’m getting lots of “redefinition of IMutex_T” name clashes due to mixing #includes and modules)