1

I have the following code:

std::ifstream ifs(fileName, std::ios_base::in | std::ios_base::binary);
std::vector<char> vecdata(std::istreambuf_iterator<char>(ifs), 
                          std::istreambuf_iterator<char>());
image->setData(std::move(vecdata));

setData is declared as follows:

void Image::setData(std::vector<char> && data);

I have the following error:

reader.cpp:752: error: no matching function for call to

'Image::setData(std::vector (&)(std::istreambuf_iterator >, std::istreambuf_iterator > (*)()))'

image->setData(std::move(vecdata));
                                 ^

candidate is:

image.h:12: void Image::setData(std::vector&&)

I am using gcc 4.91

MCVE with gcc 5.4, same behavior : http://rextester.com/QYA50737

Is this a gcc bug?

Thanks

galinette
  • 8,896
  • 2
  • 36
  • 87
  • 3
    Most Vexing Parse is the key. – Edgar Rokjān Mar 14 '17 at 09:54
  • http://stackoverflow.com/questions/5363748/constructor-not-returning-usable-object – halfelf Mar 14 '17 at 09:56
  • I got the idea by searching for most vexing parse. The second parameter can be interpreted as an unnamed pointer to a function returning a istreambuf_iterator. But how can the first parameter be interpreted as a function declaration argument? To me it can only be a rvalue – galinette Mar 14 '17 at 14:57

1 Answers1

5

You got Most Vexing Parse: use {} instead of ():

std::vector<char> vecdata{std::istreambuf_iterator<char>(ifs), 
                          std::istreambuf_iterator<char>()};
Jarod42
  • 203,559
  • 14
  • 181
  • 302