18

The following code was compiled with VC++ 2012:

void f1(void (__stdcall *)())
{}

void f2(void (__cdecl *)())
{}

void __cdecl h1()
{}

void __stdcall h2()
{}

int main()
{
    f1(h1); // error C2664
    f2(h2); // error C2664

    f1([](){}); // OK
    f2([](){}); // OK

    auto fn = [](){};

    f1(fn); // OK
    f2(fn); // OK
}

I think the errors are normal yet the OKs are abnormal.

So, my questions are:

  1. What's the calling convention of a C++ lambda function?

  2. How to specify the calling convention of a C++ lambda function?

  3. If the calling convention is not defined, how to correctly recycle the stack space after having called a lambda function?

  4. Does the compiler automatically generate multiple versions of a lambda function? i.e. as the following pseudo-code:

    [] __stdcall (){};

    [] __cdecl (){}; etc.

11
  • 3
    Linking this question seems useful: stackoverflow.com/questions/14169295/…
    – jogojapan
    Commented Feb 13, 2013 at 3:20
  • f1 is using __stdcall but h1 is using __cdecl; if you swap those around does it work? Commented Feb 13, 2013 at 3:24
  • 5
    Since the C++ standard doesn't mention anything about calling conventions, the answer to your first question would seem to become compiler specific.
    – Jesse Good
    Commented Feb 13, 2013 at 3:33
  • 3
    I think the question linked by @jogojapan answers this question. Looking at the symbol dump posted in that one it seems VC generates function call operators with all 3 calling conventions when you define a lambda. It'll link the most appropriate match based on the how the lambda is being called.
    – Praetorian
    Commented Feb 13, 2013 at 3:35
  • 1
    @jogojapan, I'm sorry for my overlook of the link you provided. I have upvoted your comment and deleted my first comment.
    – xmllmx
    Commented Feb 13, 2013 at 3:45

2 Answers 2

16

On VC++ 2012, compiler choose automatically calling conversion for stateless lambdas (that has no capture variables) when you convert "stateless lambda to function pointer".

MSDN C++11 Features:

Lambdas

[...] Additionally in Visual C++ in Visual Studio 2012, stateless lambdas are convertible to function pointers. [...] (The Visual C++ in Visual Studio 2012 is even better than that, because we've made stateless lambdas convertible to function pointers that have arbitrary calling conventions. This is important when you are using APIs that expect things like __stdcall function pointers.)


EDITED:

NB: The calling conversion is out of C++ Standard, it depends on other specification such as platform ABI(application binary interface).

The following answers are based on output assembly code with /FAs compiler option. So it's a mere guess, and please ask Microsoft for more detail ;P

Q1. What's the calling convention of a C++ lambda function?

Q3. If the calling convention is not defined, how to correctly recycle the stack space after having called a lambda function?

First of all, C++ lambda(-expression) is NOT a function (nor function pointer), you can call operator() to lambda object like a calling normal function. And output assembly code says that VC++ 2012 generates lambda-body with __thiscall calling conversion.

Q2. How to specify the calling convention of a C++ lambda function?

AFAIK, there is no way. (It may be only __thiscall)

Q4. Does the compiler automatically generate multiple versions of a lambda function? i.e. as the following pseudo-code: [...]

Probably No. The VC++ 2012 lambda-type provides only one lambda-body implementation (void operator()()), but provides multiple "user-defined conversion to function pointer" for each calling conversion (operator return function pointer with void (__fastcall*)(void), void (__stdcall*)(void), and void (__cdecl*)(void) type).

Here is an example;

// input source code
auto lm = [](){ /*lambda-body*/ };

// reversed C++ code from VC++2012 output assembly code
class lambda_UNIQUE_HASH {
  void __thiscall operator()() {
    /* lambda-body */
  }
  // user-defined conversions
  typedef void (__fastcall * fp_fastcall_t)();
  typedef void (__stdcall * fp_stdcall_t)();
  typedef void (__cdecl * fp_cdecl_t)();
  operator fp_fastcall_t() { ... }
  operator fp_stdcall_t() { ... }
  operator fp_cdecl_t() { ... }
};
lambda_UNIQUE_HASH lm;
7
  • 2
    +1 for the reference (although the answer doesn't cover all aspects of the question).
    – jogojapan
    Commented Feb 13, 2013 at 3:44
  • 5
    @jogojapan: It pretty much covers everything, since a lambda function is still ultimately a member function. And a member function doesn't really have calling conventions. Not in the __cdecl way. Since calling conventions are platform-specific anyway, it's up to each platform to decide how it works. Microsoft, displaying a shocking degree of competence, decided on the most useful way. Commented Feb 13, 2013 at 4:02
  • @NicolBolas What I mean is mainly that the quoted text doesn't seem to provide a clear answer to part 2 of the question. The description above says that lambdas are by default convertible to all calling conventions, but that doesn't necessarily imply that there is no way to specify the convention if one wanted to. It would be VC-specific syntax, of course. (I am not sure if I understand your point about member functions. Lambdas are always member functions.. why is that?)
    – jogojapan
    Commented Feb 13, 2013 at 4:15
  • 1
    @jogojapan: "Lambdas are always member functions.. why is that?" Because lambda functions in C++ are functors; they're objects. They have an operator() which is where your function goes. Even capture-less lambdas are objects. The are convertible to function pointers, but they aren't function pointers. They're objects. Except for the convertibility, a lambda is no different functionally from struct Nameless {Ret operator()(...) {...}};. Indeed, the standard requires that it is implemented this way, with a typename and everything. Commented Feb 13, 2013 at 4:19
  • 1
    Also see this reference Commented Nov 16, 2016 at 22:02
3

A stateless lambda function is still a class, but a class that can be implicitly converted into a function pointer.

The C++ standard doesn't cover calling conventions, but there is little reason why a stateless lambda could not create a wrapper in any calling convention that forwards through to the stateless lambda when the lambda is converted to a function pointer.

As an example, we could do this:

#include <iostream>

void __cdecl h1() {}
void __stdcall h2(){}

// I'm lazy: 
typedef decltype(&h1) cdecl_nullary_ptr;
typedef decltype(&h2) stdcall_nullary_ptr;

template<typename StatelessNullaryFunctor>
struct make_cdecl {
  static void __cdecl do_it() {
    StatelessNullaryFunctor()();
  }
};
template<typename StatelessNullaryFunctor>
struct make_stdcall {
  static void __stdcall do_it() {
    StatelessNullaryFunctor()();
  }
};

struct test {
  void operator()() const { hidden_implementation(); }

  operator cdecl_nullary_ptr() const {
    return &make_cdecl<test>::do_it;
  }
  operator stdcall_nullary_ptr() const {
    return &make_stdcall<test>::do_it;
  }
};

where our test stateless nullary class can be converted into both a cdecl and stdcall function pointer implicitly.

The important part of this is that the calling convention is part of the type of the function pointer, so operator function_type knows what calling convention is being requested. And with perfect forwarding, the above can even be efficient.

2
  • 1
    A lambda function is a class?
    – jogojapan
    Commented Feb 13, 2013 at 3:45
  • 5
    @jogojapan: It probably should say, a stateless lambda is still an object, but an object which can be converted into a function pointer.
    – Jesse Good
    Commented Feb 13, 2013 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.