mardi 29 mars 2016

How to emplace a function wrapper with some arguments from local variables into container with MSVC2015?

I was looking at an answer to another question. However I can't figure out, based on that example, why can't I bind a value of some local variable with MSVC 2015 compiler? It just throws an error while gcc 5.3 compiles it fine on msys2/mingw64. I mean like in

#include <iostream>
#include <functional>
#include <vector>

int add(int a, int b) { return a + b; }

using bound_add_t = decltype(std::bind(add, std::placeholders::_1, int()));

int main() {
  std::vector<bound_add_t> vec;
  int y = 2;
  vec.emplace_back(add,std::placeholders::_1, y); // <- this causes the problem
  vec.emplace_back(add,std::placeholders::_1, 2);
  vec.emplace_back(add,std::placeholders::_1, 3);

  for (auto &b : vec)
    std::cout << b(5) << std::endl;

  return 0;   
}

Severity Code Description Project File Line Suppression State Error C2664 'std::_Binder &,int>::_Binder(std::_Binder &,int> &&)': cannot convert argument 3 from 'int' to 'int &&' i2pd C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\xmemory0 655

Is it a known issue tracked somewhere? I'm not even sure what is the underlying problem here. Is there a workaround?

In my use case, I'm missing one argument that becomes available later, so I'd want to have a vector with a wrapped function ready just like in that example.

Aucun commentaire:

Enregistrer un commentaire