llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.84k stars 11.47k forks source link

Templated lambda invoked in compile time doesn't instantiate if declared inside templated function #100897

Open GuzevArtem opened 1 month ago

GuzevArtem commented 1 month ago

Example below compiles fine under MSVC and GCC

template<typename>
[[nodiscard]]
constexpr auto foo() noexcept
{
    constexpr auto extract_size = []<typename argument_t>() constexpr -> int
    {
        return 1;
    };
    //return extract_size.template operator()<int>(); // works
    constexpr int result = extract_size.template operator()<int>(); // undefined function 'operator()<int>'
    return result;
}

int main() {
    return foo<void>();
}

If foo isn't templated, it compiles fine.

https://godbolt.org/z/6bj4d6zef

:10:19: error: constexpr variable 'result' must be initialized by a constant expression :10:50: note: undefined function 'operator()' cannot be used in a constant expression

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-frontend

Author: None (GuzevArtem)

Example below compiles fine under MSVC and GCC ``` template<typename> [[nodiscard]] constexpr auto foo() noexcept { constexpr auto extract_size = []<typename argument_t>() constexpr -> int { return 1; }; //return extract_size.template operator()<int>(); // works constexpr int result = extract_size.template operator()<int>(); // undefined function 'operator()<int>' return result; } int main() { return foo<void>(); } ``` If foo isn't templated, it compiles fine. https://godbolt.org/z/6bj4d6zef <source>:10:19: error: constexpr variable 'result' must be initialized by a constant expression <source>:10:50: note: undefined function 'operator()<int>' cannot be used in a constant expression
zyn0217 commented 1 month ago

At first glance this is likely a duplicate of https://github.com/llvm/llvm-project/issues/35052

GuzevArtem commented 1 month ago

For future readers WA - remove explicit lambda return type more details: https://github.com/llvm/llvm-project/issues/35052#issuecomment-1797064371