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?
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
Actually, there are two differences between the code segments:
long
and no parenthesis after the use of g_Frequency
, merely reading its value.g_Frequency
becomes a function object and there are parenthesis after the use of g_frequency
making it a call to a function.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With