Skip to main content
added 4 characters in body
Source Link
wohlstad
  • 35.8k
  • 18
  • 78
  • 111

Let's say that we have a simple main.cpp file which contains only a single call of a function bar which is defined in the module a:

import a;

int main() {
    bar(1);
}

Then let's take a look at the module a:

export module a;

namespace N {

struct adl_tag {};

void foo(adl_tag, auto) {}

} // namespace N

export void bar(auto v) {
    return foo(N::adl_tag{}, v);
}

There we have an exported function template bar, which invokes N::foo using argument-dependent lookup. The function foo, however, is not exported and is not visible from the outside code.

Godbolt link: https://godbolt.org/z/6Waqbj6zh

clang 21 compiles this project just fine but gcc 15 fails with an error:

In instantiation of ‘void bar@a(auto:2) [with auto:2 = int]’:
error: ‘foo’ was not declared in this scope
In instantiation of ‘void bar@a(auto:2) [with auto:2 = int]’:
error: ‘foo’ was not declared in this scope

Which compiler is right here?

Let's say that we have a simple main.cpp file which contains only a single call of a function bar which is defined in the module a:

import a;

int main() {
    bar(1);
}

Then let's take a look at the module a:

export module a;

namespace N {

struct adl_tag {};

void foo(adl_tag, auto) {}

} // namespace N

export void bar(auto v) {
    return foo(N::adl_tag{}, v);
}

There we have an exported function template bar, which invokes N::foo using argument-dependent lookup. The function foo, however, is not exported and is not visible from the outside code.

Godbolt link: https://godbolt.org/z/6Waqbj6zh

clang 21 compiles this project just fine but gcc 15 fails with an error:

In instantiation of ‘void bar@a(auto:2) [with auto:2 = int]’:
error: ‘foo’ was not declared in this scope

Which compiler is right here?

Let's say that we have a simple main.cpp file which contains only a single call of a function bar which is defined in the module a:

import a;

int main() {
    bar(1);
}

Then let's take a look at the module a:

export module a;

namespace N {

struct adl_tag {};

void foo(adl_tag, auto) {}

} // namespace N

export void bar(auto v) {
    return foo(N::adl_tag{}, v);
}

There we have an exported function template bar, which invokes N::foo using argument-dependent lookup. The function foo, however, is not exported and is not visible from the outside code.

Godbolt link: https://godbolt.org/z/6Waqbj6zh

clang 21 compiles this project just fine but gcc 15 fails with an error:

In instantiation of ‘void bar@a(auto:2) [with auto:2 = int]’:
error: ‘foo’ was not declared in this scope

Which compiler is right here?

Source Link
siga
  • 451
  • 3
  • 11

Can an exported function template invoke a module-local function using ADL?

Let's say that we have a simple main.cpp file which contains only a single call of a function bar which is defined in the module a:

import a;

int main() {
    bar(1);
}

Then let's take a look at the module a:

export module a;

namespace N {

struct adl_tag {};

void foo(adl_tag, auto) {}

} // namespace N

export void bar(auto v) {
    return foo(N::adl_tag{}, v);
}

There we have an exported function template bar, which invokes N::foo using argument-dependent lookup. The function foo, however, is not exported and is not visible from the outside code.

Godbolt link: https://godbolt.org/z/6Waqbj6zh

clang 21 compiles this project just fine but gcc 15 fails with an error:

In instantiation of ‘void bar@a(auto:2) [with auto:2 = int]’:
error: ‘foo’ was not declared in this scope

Which compiler is right here?