fork(4) download
  1. #include <iostream>
  2. using namespace std;
  3. #ifdef _MSC_VER
  4. #define __PRETTY_FUNCTION__ __FUNCSIG__
  5. #endif
  6. class Foo
  7. {
  8. public:
  9. Foo()
  10. {
  11. cout << __PRETTY_FUNCTION__ << endl;
  12. }
  13. Foo(Foo&&)
  14. {
  15. cout << __PRETTY_FUNCTION__ << endl;
  16. }
  17. explicit Foo(const Foo&)
  18. {
  19. cout << __PRETTY_FUNCTION__ << endl;
  20. }
  21.  
  22. };
  23. void Bar(Foo & a) {}
  24. int main() {
  25. const Foo f;
  26. const Foo &f2 = f;
  27. cout << "@@@@@@+" << endl;
  28. auto lambda = [f2, f3(f2)]() mutable {
  29. Bar(f3); //why f3沒保留const
  30. Bar(f2); //f2確有
  31. };
  32. cout << "@@@@@@-" << endl;
  33. lambda();
  34. // your code goes here
  35. return 0;
  36. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In lambda function:
prog.cpp:30:11: error: binding 'const Foo' to reference of type 'Foo&' discards qualifiers
     Bar(f2); //f2確有
           ^
prog.cpp:23:6: note:   initializing argument 1 of 'void Bar(Foo&)'
 void Bar(Foo & a) {}
      ^
prog.cpp: In function 'int main()':
prog.cpp:28:17: error: cannot bind 'const Foo' lvalue to 'Foo&&'
   auto lambda = [f2, f3(f2)]() mutable {
                 ^
prog.cpp:13:3: note:   initializing argument 1 of 'Foo::Foo(Foo&&)'
   Foo(Foo&&)
   ^
stdout
Standard output is empty