Skip to content

[clang] Linker failure for std::visit with a templated lambda using a forward declared templated function #115731

@StefanPaulet

Description

@StefanPaulet
#include <type_traits>
#include <variant>

using MyType = std::variant<double, int>;

template <typename T>
auto foo(T&& arg) -> std::remove_cvref_t<T>;

auto bar(MyType const& v) {
    auto callable = []<typename Arg>(Arg&& arg) -> MyType {
        return foo(std::forward<Arg>(arg));
    };
    return std::visit(callable, v);
}

template <typename T>
auto foo(T&& arg) -> std::remove_cvref_t<T> {
    return arg;
}

int main() {
    MyType b {5.3};
    bar(b);
}

https://godbolt.org/z/n54f19hn5

It seems that only the declarations get generated for foo, but no definitions. However, if foo is no longer forward declared, everything works: https://godbolt.org/z/c1WY647af.

Diagnostics:

/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/example-c83671.o: in function `std::variant<double, int> bar(std::variant<double, int> const&)::$_0::operator()<double const&>(double const&) const':
<source>:11:(.text+0x315): undefined reference to `std::remove_cvref<double const&>::type foo<double const&>(double const&)'
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/15.0.0/../../../../x86_64-linux-gnu/bin/ld: /tmp/example-c83671.o: in function `std::variant<double, int> bar(std::variant<double, int> const&)::$_0::operator()<int const&>(int const&) const':
<source>:11:(.text+0x3f5): undefined reference to `std::remove_cvref<int const&>::type foo<int const&>(int const&)'

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions