2
#include <vector>                                                               

void main() {                                                                   
  std::vector<int> test[2];                                                     
  auto funct = [test](){ test[0].push_back(1); };                               
  funct();                                                                      
} 

As a result I get

main.cc:5:45: error: passing ‘const std::vector’ as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator; std::vector<_Tp, _Alloc>::value_type = int]’ discards qualifiers [-fpermissive] auto funct = test{ test[0].push_back(1); };

How can I capture test pointer without making its values const? Is there any other way other than making it a vector<vector<int>>? Why is it even becoming a const?

user364622
  • 366
  • 1
  • 7
  • 18
  • And to the point that isn't a direct duplicate: `test` is _not a pointer_. It's an array. There are lots of situations where arrays decay to pointers, but this isn't one of them (and your array is copied by value). – Useless Jun 06 '17 at 10:11

2 Answers2

3
#include <vector>                                                               

int main() {                                                                   
  std::vector<int> test[2];                                                     
  auto funct = [test]() mutable { test[0].push_back(1); };                               
  funct();                                                                      
} 
3

You can try this one.

#include <vector>                                                               

int main() {                                                                   
  std::vector<int> test[2];                                                     
  auto funct = [&test](){ test[0].push_back(1); };                               
  funct();
  return 0;                                                                      
}