5

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?

3
  • 1
    One has HERE the other Here :) Commented Dec 31, 2013 at 6:21
  • Ahahaha.. +1 for humor. I meant the bracket though. It's my first time seeing a bracket after a lambda and I can't find any other examples online. Commented Dec 31, 2013 at 6:22
  • I wish I knew what the difference is, I never saw "auto", so I am not sure what it does, nor what = [] () is .. sorry, usually () at the end mean execute, rather than assign values Commented Dec 31, 2013 at 6:23

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You! For the life of me, I could not see that. So with the brackets g_Frequency is a variable of return type and without the brackets g_Frequency is a lambda? EDIT: I'll accept this as soon as it lets me
@CantChooseUsernames yeah, I included an example
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.