Skip to content

[C++20] [Modules] Errors in exporting forward declared structs #102349

@davidstone

Description

@davidstone

Given the following translation units:

export module a;

export template<typename>
struct a;

template<typename>
struct a {
};

export template<typename T>
constexpr auto aa = a<T>();
export module b;

import a;

static void b() {
	static_cast<void>(a<void>());
}
export module c;

import a;

static void c() {
	static_cast<void>(aa<void>);
}
import a;
import b;
import c;

static void d() {
	static_cast<void>(a<void>());
}

clang erroneously rejects with

In file included from /app/d.cpp:1:
a.cpp:7:8: error: declaration 'a<void>' attached to named module 'a' can't be attached to other modules
    7 | struct a {
      |        ^
a.cpp:7:8: note: also found
a.cpp:7:8: error: declaration 'a' attached to named module 'a' can't be attached to other modules
    7 | struct a {
      |        ^
a.cpp:7:8: note: also found
a.cpp:7:8: error: declaration 'a' attached to named module 'a' can't be attached to other modules
    7 | struct a {
      |        ^
a.cpp:7:8: note: also found
a.cpp:7:8: error: declaration 'a' attached to named module 'a' can't be attached to other modules
    7 | struct a {
      |        ^
a.cpp:7:8: note: also found
a.cpp:7:8: error: declaration '~a' attached to named module 'a' can't be attached to other modules
    7 | struct a {
      |        ^
a.cpp:7:8: note: also found
5 errors generated.

See it live: https://godbolt.org/z/EM9dh38WP

As a workaround, users must type export on both the forward declaration and the definition:

export module a;

export template<typename>
struct a;

export template<typename>
struct a {
};

export template<typename T>
constexpr auto aa = a<T>();

Metadata

Metadata

Assignees

Labels

clang:modulesC++20 modules and Clang Header Modules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions