3

I'm following the example at the following link:

https://www.boost.org/doc/libs/1_55_0/libs/math/doc/html/math_toolkit/high_precision/use_multiprecision.html

I get an error on the following line:

[&n](cpp_dec_float_50& y)


g++ -I ../boost_1_71_0 fft.cpp -o fft
fft.cpp:52:3: error: expected expression
  [&n](cpp_dec_float_50& y)
  ^
1 error generated.

The full block is:

// Generate the sine values.
std::for_each
(
  sin_values.begin (),
  sin_values.end (),
  [&n](cpp_dec_float_50& y)
  {
    y = sin( pi<cpp_dec_float_50>() / pow(cpp_dec_float_50 (2), n));
    ++n;
  }
);

What is "[&n](cpp_dec_float_50& y)" actually doing? And why is it erroring?

Joe
  • 874
  • 1
  • 9
  • 25
  • That is a lambda expression. Make sure you have c++11 enabled. [https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – drescherjm Oct 04 '19 at 22:06

1 Answers1

2

What is [&n](cpp_dec_float_50& y) actually doing?

It's the first part of a lambda expression, i.e. an anonymous function.

And why is it erroring?

You need to compile for C++11 (or higher). Use -std=c++11 (or -std=c++14 or -std=c++17) in your compiler command line. eg:

g++ -std=c++11 ...
Paul Evans
  • 26,111
  • 3
  • 30
  • 50
  • That worked! I've heard of lambda functions on AWS, I didn't realize there was a declaration in C++ for them. I wasn't sure what to look up, thanks. – Joe Oct 04 '19 at 22:19
  • 1
    You're totally welcome. Check them out, they're very handy for using STL algorithms, *etc*. – Paul Evans Oct 04 '19 at 22:20
  • "Lambda functions on AWS" are not really lambda functions, in the same sense that "good vibrations" are not physical vibrations. "AWS Lambda" is just the name AWS chose (by loose analogy) for a particular kind of AWS service that lets you write small pieces of code and have them executed as private microservices within your AWS stack (e.g. a thumbnailer). The loose analogy is that lambda functions let you write a function definition at the point of use without having to define it separately, while AWS Lambda "lets you run code without provisioning or managing servers". – Szczepan Hołyszewski Feb 22 '20 at 16:55