Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

038-move-semantics.md の「コピーとムーブの判別」でobjectがlambdaにcaptureされていない #155

Open
sylph01 opened this issue May 16, 2021 · 0 comments

Comments

@sylph01
Copy link
Contributor

sylph01 commented May 16, 2021

再現コード

#include "../all.h"

void f(const int &){
  std::cout << "lvalue\n";
}
void f(const int &&){
  std::cout << "rvalue\n";
}

int main(){
  int object {};

  f(object);
  f(object + object);
  f([&]{ return object; }());
  f(std::move(object));
}

実行結果

$ g++ -std=c++17 -Wall lvalue-and-rvalue.cpp
lvalue-and-rvalue.cpp: In lambda function:
lvalue-and-rvalue.cpp:15:16: error: ‘object’ is not captured
   15 |   f([]{ return object; }());
      |                ^~~~~~
lvalue-and-rvalue.cpp:15:6: note: the lambda has no capture-default
   15 |   f([]{ return object; }());
      |      ^
lvalue-and-rvalue.cpp:11:7: note: ‘int object’ declared here
   11 |   int object {};
      |       ^~~~~~

おまけ

https://twitter.com/TartanLlama/status/1283816537778982914 を思い出した。
試しに objectconstexpr にしたらコンパイルされた。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant