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

Discussion in 'C++' started by Victor Bazarov, Apr 16, 2013.

  1. 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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 16, 2013
    #1
    1. Advertising

  2. On 4/16/2013 5:40 PM, Sergiy Kanilo wrote:
    > Victor Bazarov <> wrote in news:kkkema$3u2$:
    >
    >> 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!

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 17, 2013
    #2
    1. Advertising

  3. Victor Bazarov

    Stuart Guest

    On 04/17/2013, Victor Bazarov wrote:
    > On 4/16/2013 5:40 PM, Sergiy Kanilo wrote:
    >> Victor Bazarov <> wrote in
    >> news:kkkema$3u2$:
    >>
    >>> 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
     
    Stuart, Apr 18, 2013
    #3
  4. Victor Bazarov

    Luca Risolia Guest

    On 18/04/2013 08:34, Stuart wrote:
    > 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
    > code, you can find the error easily in the debugger.
     
    Luca Risolia, Apr 18, 2013
    #4
  5. Victor Bazarov

    Balog Pal Guest

    On 4/18/2013 8:34 AM, Stuart wrote:
    > 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
     
    Balog Pal, Apr 18, 2013
    #5
  6. Victor Bazarov

    Balog Pal Guest

    On 4/18/2013 6:53 PM, Luca Risolia wrote:
    > On 18/04/2013 08:34, Stuart wrote:
    >> 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
    >> code, you can find the error easily in the debugger.


    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.
     
    Balog Pal, Apr 18, 2013
    #6
  7. Victor Bazarov

    Stuart Guest

    On 4/18/2013, Stuart wrote:
    >> 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.



    On 04/18/13, Balog Pal wrote:
    > 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


    That looks really promising.

    Thanks for sharing,
    Stuart
     
    Stuart, Apr 18, 2013
    #7
  8. Victor Bazarov

    gwowen Guest

    On Apr 19, 9:51 am, Juha Nieminen <> wrote:
    > Andy Champ <> wrote:
    > > I wish I could agree with you.  Not merely are template error messages
    > > often obscure, but I have had instances where no line of my code is
    > > mentioned anywhere in the error.

    >
    > 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)
     
    gwowen, Apr 19, 2013
    #8
  9. Victor Bazarov

    osmium Guest

    "Victor Bazarov" wrote:

    > On 4/19/2013 9:04 AM, gwowen wrote:
    >> On Apr 19, 9:51 am, Juha Nieminen <> wrote:
    >>> Andy Champ <> wrote:
    >>>> I wish I could agree with you. Not merely are template error messages
    >>>> often obscure, but I have had instances where no line of my code is
    >>>> mentioned anywhere in the error.
    >>>
    >>> 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)

    >
    > 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.
     
    osmium, Apr 19, 2013
    #9
  10. On 4/22/2013 9:52 AM, Andy Champ wrote:
    > On 22/04/2013 10:10, Juha Nieminen wrote:
    >> gwowen <> wrote:
    >>> On Apr 19, 9:51?am, Juha Nieminen <> wrote:
    >>>> Andy Champ <> wrote:
    >>>>> I wish I could agree with you. ?Not merely are template error messages
    >>>>> often obscure, but I have had instances where no line of my code is
    >>>>> mentioned anywhere in the error.
    >>>>
    >>>> 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)

    >>
    >> Which part of "ignore the template error messages and look at the first
    >> error message that refers to your code" did you not understand?
    >>

    >
    > 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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 22, 2013
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aliki
    Replies:
    3
    Views:
    787
    LostAtC
    Sep 24, 2004
  2. Roman Suzi
    Replies:
    13
    Views:
    629
    Bengt Richter
    Jan 7, 2005
  3. Steven
    Replies:
    0
    Views:
    406
    Steven
    Nov 30, 2005
  4. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    203
    Sean O'Halpin
    Mar 30, 2009
  5. Haochen Xie
    Replies:
    4
    Views:
    262
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page