C++ Logo

std-proposals

Advanced search

[std-proposals] sizeof lambda in an unevaluated context

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 24 Jun 2024 23:54:35 +0100
Consider the following program:

    #include <cstddef>
    #include <iostream>

    template<std::size_t n>
    struct S {
        inline static constexpr std::size_t m = n;
        template<typename... Params>
        S(Params&&...){}
    };

    template<typename... Params>
    S(Params&&... args) -> S< sizeof([&args...](){}) >;

    int main(void)
    {
        S var(1,2,3,4,5,6,7,8,9);
        std::cout << var.m << std::endl;
    }

This program prints out 1 on both LLVM clang and Intel ICX, see here:

    https://godbolt.org/z/rE3745cxG

It causes a segfault on the GNU g++ compiler (i.e. the compiler itself
segfaults).

If we are allowed since C++20 to have lambdas in unevaluated contexts,
then does it make sense to introduce a few new restrictions? Such as:
    (1) The size of two lambda types that have the same captures shall
be the same
    (2) The alignment of two lambda types that have the same captures
shall be the same

Received on 2024-06-24 22:54:48