Getting an error with lambda and async - help interpret, please

V

Victor Bazarov

Hello,

I'm studying lambdas and threading feature in C++11. Using some
materials from different sources, and feeling my way through...

Here is the code that gives me the trouble today:
==================================================== >8 cut here
#include <future>

int main()
{
auto foo = [](int i, int j) -> float {
float sum = 0;
for (int k = 0; k < i*j; k++)
sum += k*k;
return sum;
};

auto brr = foo(5,6); // OK
auto bar = std::async(foo(5, 6)); // line 13
}
==================================================== >8 cut here

I get a long-nested error message for line 13 from VC++ 2012 Express,
and from what I could gather it didn't like my 'return' statement or
something else. Do you see anything wrong with the code? I don't have
a way to try it on any other compiler (maybe clang? I'll try...)

Thanks!

V
 
S

Stuart

Here is the code that gives me the trouble today: [...]
auto bar = std::async(foo(5, 6)); // line 13

shouldn't it be

auto bar = std::async( foo, 5, 6 );

D-oh!

This is an example that will turn off many people from programming C++.
If not even very experienced programmers (I would even consider Victor
a guru) can decipher the error messages of templated code, then who can?

Admittedly, even the inventor of C++ didn't have such monstrosities like
boost in mind when he introduced templates. However, we need some kind
of template debugging facilities in the language: if you write wrong
code, you can find the error easily in the debugger. Unfortunately there
is no "debugger" for templates. Either you get it just right or you'll
get a bunch of incomprehensible error messages. I don't know how many
hours I have spent to get the most simple boost examples to work.

Regards and sympathy,
Stuart
 
L

Luca Risolia

Admittedly, even the inventor of C++ didn't have such monstrosities like
boost in mind when he introduced templates. However, we need some kind
of template debugging facilities in the language:

like "Constraints" (or "Concept light") ? There is an interesting
article written by Stroustrup and others available somewhere at
isocpp.org. If I remember well, some patches should be
already available for gcc 4.8. Try them out and let the creator of C++
know your impressions.

if you write wrong
 
B

Balog Pal

This is an example that will turn off many people from programming C++.
If not even very experienced programmers (I would even consider Victor
a guru) can decipher the error messages of templated code, then who can?

Admittedly, even the inventor of C++ didn't have such monstrosities like
boost in mind when he introduced templates.

For the record, the inventor of C++ did meet a plenty of "interesting"
error messages, and in fis prez a week ago referred to one that exceeded
the line limit of his cygwin terminal, so he could not even look at the
start of it.

The problem was to be addressed long time ago, with 'Concepts', that
after 7+ years of work and several iterations got pulled from the
standard WP around 2008.

The good new is that the new wave of this, 'Concepts Lite' was recently
introduced and has good likehood to start official life as a standard
TS. And can get ready around the same time as C++14. Also many compiler
writers said they will implement it very soon, so chances are good the
language will be capable for good messages before calendar turns to '15.
The standard library is a different tack but I expect it to follow shortly.
However, we need some kind
of template debugging facilities in the language: if you write wrong
code, you can find the error easily in the debugger. Unfortunately there
is no "debugger" for templates. Either you get it just right or you'll
get a bunch of incomprehensible error messages. I don't know how many
hours I have spent to get the most simple boost examples to work.

Unfortunately without express constraints the compiler really is only
hit by some chance error, and can't do much better job than report that
and dump all the contexts. That is the very thing to be changed.

Look here for the initial version:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3580.pdf
 
B

Balog Pal

like "Constraints" (or "Concept light") ? There is an interesting
article written by Stroustrup and others available somewhere at
isocpp.org. If I remember well, some patches should be
already available for gcc 4.8. Try them out and let the creator of C++
know your impressions.

if you write wrong

Andrew Sutton implemented the stuff in Concepts Lite on an experimental
branch of gcc -- not sure if it is made public right ahead, but likely
it will become available for experimenting.
 
S

Stuart

For the record, the inventor of C++ did meet a plenty of "interesting"
error messages, and in fis prez a week ago referred to one that exceeded
the line limit of his cygwin terminal, so he could not even look at the
start of it.

The problem was to be addressed long time ago, with 'Concepts', that
after 7+ years of work and several iterations got pulled from the
standard WP around 2008.

The good new is that the new wave of this, 'Concepts Lite' was recently
introduced and has good likehood to start official life as a standard
TS. And can get ready around the same time as C++14. Also many compiler
writers said they will implement it very soon, so chances are good the
language will be capable for good messages before calendar turns to '15.
The standard library is a different tack but I expect it to follow shortly.


Unfortunately without express constraints the compiler really is only
hit by some chance error, and can't do much better job than report that
and dump all the contexts. That is the very thing to be changed.

Look here for the initial version:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3580.pdf

That looks really promising.

Thanks for sharing,
Stuart
 
G

gwowen

I just love how people always bring up extremely rare fringe cases as
counter-arguments to "in the majority of cases, X."

"Not merely are template error messages **often** obscure" (my
emphasis)
 
O

osmium

Victor Bazarov said:
Oh, so you want another loop around this? They are. But in the majority
of cases you can ignore all those and *concentrate on your code* that
causes the error message. If I weren't a novice with 'std::async'
template, concentrating on my own line of code ought to give me the actual
idea what was wrong. And that's how Sergiy helped me - by looking at *my*
code. So, "in the majority of cases, look at your code", and the
counter-argument about "instances where no line of my code is mentioned
anywhere" just doesn't stand *because* it's a fringe case, extremely rare.
That's what Juha is saying. You sound like you want to argue with Juha
about that. With what?

Years ago, there was a Basic interpreter that had one favorite (perhaps the
only) error message, "Something is wrong".

As I see it, the *practical* advice is to read the message as if it said
"Something is wrong". That's not criticism, it's just my distillation of the
thread.
 
V

Victor Bazarov

Which part of "I have had instances where no line of my code is
mentioned anywhere in the error" did _you_ not understand?

I stand by my previous response: "No, read it. It may make sense. It
will usually point you to the right line in your code.

Just not always."

I think it's time some proof be presented. The "look at the error
message that refers to *your* code" needs no proof. It's self evident.
With an exception of the obvious situation when some part of the
library code is incorrectly compiled due to some macro set in your code
or in your compiler settings, what other situation *might* there be that
would produce an error in the library *without* referring to any of
*your* (or *my*, or *his*) code?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top