public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/61402] New: [C++1y] Init-capture with side effect not working for some types
@ 2014-06-03 11:07 thibaut.lutz at googlemail dot com
  2014-06-03 11:15 ` [Bug c++/61402] " thibaut.lutz at googlemail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: thibaut.lutz at googlemail dot com @ 2014-06-03 11:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61402

            Bug ID: 61402
           Summary: [C++1y] Init-capture with side effect not working for
                    some types
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: thibaut.lutz at googlemail dot com

Lambda functions with an init-capture for which the initializer has a side
effect is not always taken into account.

The example below defines a lambda with an init-capture, which increments a
variable in the initalizer, and invoke the lambda immediately with the same
variable as a parameter. The body of the lambda prints the value of the
captured value and its parameters, which should be identical.

Expected result: The captured value and the parameter should have the same
value, since the instantiation of the lambda should be evaluated before its
invocation.

Actual results: for some types (at least bool and char), the increment inside
the init-capture is partially evaluated: the captured value is correct but the
side effect is gone. Other types are working as expected.

Tested with: gcc 4.9.0 and 4.8.1

Code:
--8<----8<----8<----8<----8<----8<--
#include <iostream>

template<typename T>
void foo(T t) {
  using namespace std;
  // print the original argument
  cout << endl << "par t = " << t << endl;

  { // lambda function declaration and invocation 
    // capture argument by-copy after pre-increment 
    [ i = ++t ]
    // take one parameter
    (T v)
    {
      // print captured value and argument
      cout << "cap i = ++t = " << i << endl
           << "arg v = " << v << endl;
    }
    // invocation with the new value of the argument
    (t);
  }
}

int main(){
  foo(3.14f); // works OK
  foo(0);     // works OK
  foo('a');   // fails!
  foo(false); // fails!
}
--8<----8<----8<----8<----8<----8<--

Program output:
par t = 3.14
cap i = ++t = 4.14
arg v = 4.14

par t = 0
cap i = ++t = 1
arg v = 1

par t = a
cap i = ++t = b
arg v = a

par t = 0
cap i = ++t = 1
arg v = 0

For the last two types (char and bool), we observe v == t instead of v == i.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-12-18 17:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-03 11:07 [Bug c++/61402] New: [C++1y] Init-capture with side effect not working for some types thibaut.lutz at googlemail dot com
2014-06-03 11:15 ` [Bug c++/61402] " thibaut.lutz at googlemail dot com
2014-06-13 21:14 ` richard-gccbugzilla at metafoo dot co.uk
2014-11-25  9:37 ` [Bug c++/61402] [5 Regression][C++1y] " paolo.carlini at oracle dot com
2014-12-12 16:43 ` jason at gcc dot gnu.org
2014-12-18 17:47 ` [Bug c++/61402] -Wsequence-point doesn't notice unsequenced lambda init and function argument jason at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).