C++ Logo

sg15

Advanced search

Re: [D3034] Module Declarations Shouldn’t be Macros

From: Michael Spencer <bigcheesegs_at_[hidden]>
Date: Fri, 3 Nov 2023 03:57:06 -0700
On Fri, Nov 3, 2023 at 12:43 AM Boris Kolpackov <boris_at_[hidden]>
wrote:

> Michael Spencer via SG15 <sg15_at_[hidden]> writes:
>
> > This paper makes the following ill-formed by forbidding macro
> expansion
> > in the name of module declarations.
> >
> > version.h:
> > #ifndef VERSION_H
> > #define VERSION_H
> >
> > #define VERSION libv5
> >
> > #endif
> >
> > lib.cppm:
> > module;
> > #include "version.h"
> > export module VERSION;
>
> What about something like this:
>
> #ifdef VERSION
> export module version;
> #else
> export module final;
> #endif
>
> Sounds like it will pose the same issue but banning it feels more
> drastic.
>

This is already banned by the grammar. You can't have an if-group that
spans a pp-module.


>
> Even the use of a macro in the module name could probably have
> plausible use-cases. Say if you are trying to make a module
> that includes a version in its name:
>
> #define VERSION 2
> #define VERSIONED_NAME(n) n ## _v2
>
> And then:
>
> export module VERSIONED_NAME(utility);
>
> #if VERSION >= 2
> export void only_available_in_v2 ();
> #endif
>

Yep, this is the use-case that's banned by my suggested change, but you can
do this via code generation if you really need it. This specific case
doesn't even need that, you can just do:

```
export module utility;
export void always_available();
```

```
export module utility_v2;
export import utility;
export void only_available_in_v2 ();
 ```

This is also backwards compatible for both API and ABI.

I can't come up with any situations where you actually need to use the
preprocessor here.

Received on 2023-11-03 10:57:22