Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. struct Foo
  2. {
  3. Foo(){}
  4. void Func(){}
  5. };
  6.  
  7. int main()
  8. {
  9. const Foo foo;
  10. [foo]() mutable { foo.Func(); };
  11. }
  12.  
  13. testcase.cpp: In lambda function:
  14. testcase.cpp:10:29: error: no matching function for call to ‘Foo::Func() const’
  15. testcase.cpp:10:29: note: candidate is:
  16. testcase.cpp:4:7: note: void Foo::Func() <near match>
  17. testcase.cpp:4:7: note: no known conversion for implicit ‘this’ parameter from ‘const Foo*’ to ‘Foo*’
  18.  
  19. testcase.cpp:10:20: error: member function 'Func' not viable: 'this' argument has type 'const Foo', but function is not marked const
  20. std::async([foo]() mutable { foo.Func(); });
  21.  
  22. struct S
  23. {
  24. void f();
  25. void fc() const;
  26. };
  27.  
  28. void g()
  29. {
  30. S s0;
  31.  
  32. // [s0] () { s0.f(); }; // error, operator() is const
  33. [s0] () { s0.fc(); }; // OK, operator() is const, S::fc is const
  34.  
  35. [s0] () mutable { s0.f(); };
  36. [s0] () mutable { s0.fc(); };
  37.  
  38. const S s1;
  39.  
  40. // [s1] () { s1.f(); }; // error, s1 is const, no matter if operator() is const
  41. [s1] () { s1.fc(); };
  42.  
  43. // [s1] () mutable { s1.f(); }; // error, s1 is const, no matter if operator() is const
  44. [s1] () mutable { s1.fc(); };
  45. }
  46.  
  47. struct Foo
  48. {
  49. Foo(){}
  50. void Func(){}
  51. };
  52.  
  53. int main()
  54. {
  55. const Foo foo;
  56. {
  57. Foo& fooo= const_cast<Foo&>(foo);
  58. [fooo]() mutable { fooo.Func(); };
  59. }
  60. }
  61.  
  62. struct Foo
  63. {
  64. Foo(){}
  65. void Func(){}
  66. };
  67.  
  68. int main()
  69. {
  70. const Foo foo;
  71. [foo = foo]() mutable { foo.Func(); };
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement