Skip to content

[C++20] [Modules] static function used in template function in C++-20 module not found #78173

Open
@zhantaram

Description

@zhantaram

Hi!

NB:
Here is a sample project, depicting the problem: https://godbolt.org/z/neo8M8orM

Imagine, that I have a static inline function foo_header, defined in some header:

// header.hpp
#pragma once

static inline int foo_header(int x) {
    return x;
}

And this function is used in a template function foo_module in some module:

// module.cpp
module;
#include "header.hpp"

export module Mod;

export template<typename T>
T foo_module(T x) {
    return foo_header(x);
}

Now I want to use latter in my main.cpp:

// main.cpp
import Mod;

int main() {
    int x = foo_module(5);
    return 0;
}

However, clang falsely claims, that there is no matching function foo_header:

In file included from /app/main.cpp:1:
module.cpp:9:12: error: no matching function for call to 'foo_header'
    9 |     return foo_header(x);
      |            ^~~~~~~~~~
main.cpp:4:13: note: in instantiation of function template specialization 'foo_module<int>' requested here
    4 |     int x = foo_module(5);
      |             ^
1 error generated.

Here are some observation, based on which, I make an assumption that this is a clang bug:

  1. This code compiles/works properly on trunk of gcc
  2. This code compiles/works properly if I explicitly cast template parameter T to int like this:
// module.cpp
export template<typename T>
T foo_module(T x) {
    return foo_header(static_cast<int>(x));
}
  1. This code compiles/works properly if I remove keyword static from foo_header function declaration

Based on those observations, I could assume that body of foo_module template function gets compiled in different translation unit, from where header was included, and therefore static specifier on foo_header hides this function

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