Share via

Use lambda as default function pointer, is this a bug of MSVC compiler?

casper 5 Reputation points
2023-05-05T07:31:37.9733333+00:00
void* ff2(int (*address)() = [] {static int x = 0; return ++x; })
{
    return address;
}

These codes above will not compile in VS2019 (/std:c++latest) .

But the following codes will compile without errors. It is a bug of compiler?

using F = int (*)();
void* ff( F address = [] {static int x = 0; return ++x; } )
{
    return address;
}
Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Minxin Yu 13,516 Reputation points Microsoft External Staff
    2023-05-05T09:11:17.66+00:00

    Hi,

    Tested the snippet below and it compiles. You could report the problem to Developer Community and post link here.

    void* ff2(int temp,int (*address)() = [] {static int x = 0; return ++x; })
    {
        return address;
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.