Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ lambda followed by () vs. lambda without ()

Tags:

c++

lambda

What is the difference between these two blocks of code?

struct HighResClock
{
    typedef long long                               rep;
    typedef std::nano                               period;
    typedef std::chrono::duration<rep, period>      duration;
    typedef std::chrono::time_point<HighResClock>   time_point;
    static const bool is_steady = true;

    static time_point now();
};


namespace
{
    auto g_Frequency = []() -> long long
    {
        std::cout<<"HERE";
        LARGE_INTEGER frequency;
        QueryPerformanceFrequency(&frequency);
        return frequency.QuadPart;
    }();
}

HighResClock::time_point HighResClock::now()
{
    LARGE_INTEGER count;
    QueryPerformanceCounter(&count);
    return time_point(duration(count.QuadPart * static_cast<rep>(period::den) / g_Frequency));
}

int main()
{
    HighResClock c;
    c.now();
    c.now();
    c.now();
}

and

struct HighResClock
{
    typedef long long                               rep;
    typedef std::nano                               period;
    typedef std::chrono::duration<rep, period>      duration;
    typedef std::chrono::time_point<HighResClock>   time_point;
    static const bool is_steady = true;

    static time_point now();
};


namespace
{
    auto g_Frequency = []() -> long long
    {
        std::cout<<"HERE";
        LARGE_INTEGER frequency;
        QueryPerformanceFrequency(&frequency);
        return frequency.QuadPart;
    };
}

HighResClock::time_point HighResClock::now()
{
    LARGE_INTEGER count;
    QueryPerformanceCounter(&count);
    return time_point(duration(count.QuadPart * static_cast<rep>(period::den) / g_Frequency()));
}

int main()
{
    HighResClock c;
    c.now();
    c.now();
    c.now();
}

In case you did not notice, the difference is the bracket below:

auto g_Frequency = []() -> long long
{
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);
    return frequency.QuadPart;
}(); //this bracket here appears in one and not the other..

I ask because the one with the bracket only prints "Here" once, whereas the other (without the bracket) prints it 3 times. What does the bracket mean and what does it do? Is there a name for this syntax with the bracket?

like image 378
Brandon Avatar asked Dec 31 '13 06:12

Brandon


2 Answers

Writing () immediately after the lambda definition []{}(); will call the lambda, so the result is of type of the return type of the lambda.

If you omit the () suffix, a lambda type (unspecified) will be returned, which is basically a callable functor.

auto result = []{ return 42; }(); // auto is integer, result has 42 in it  
auto result1 = []{ return 42; }; // auto is some unspecified lambda type  
auto result2 = result1(); // auto is integer, result2 is storing 42` 
......................^^ - this is the bracket you can also put directly after the definition of the lambda
like image 80
ScarletAmaranth Avatar answered Oct 05 '22 03:10

ScarletAmaranth


Actually, there are two differences between the code segments:

  1. One code segment has the parenthesis after the lambda immediately executing it and yielding a long and no parenthesis after the use of g_Frequency, merely reading its value.
  2. The other code segment doesn't have the parenthesis after the lambda, i.e., g_Frequency becomes a function object and there are parenthesis after the use of g_frequency making it a call to a function.
like image 34
Dietmar Kühl Avatar answered Oct 05 '22 04:10

Dietmar Kühl