1

Given these two function declarations:

void initialize(int p, std::vector<Vector3> &);
std::vector<Vector3> toNurbsCoords(std::vector<Vector3>);

why does this work

Nurbs nurbs;
std::vector<Vector3> pts = nurbs.toNurbsCoords(points);
nurbs.initialize(degree, pts);

while this throws a compile time error?

Nurbs nurbs;    
nurbs.initialize(degree, nurbs.toNurbsCoords(points));
//error: no matching function for call to 'Nurbs::initialize(int&, std::vector<Vector3>)'
rubenvb
  • 74,642
  • 33
  • 187
  • 332
cangrejo
  • 2,189
  • 3
  • 24
  • 31

1 Answers1

3

Because a temporary can't bind to a non-const reference.

nurbs.toNurbsCoords(points) is a temporary. In the first case you initialize named object - pts - with it and pass that. In the second case, you just pass the temp.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Small correction: "a temporary can't bind to a non-const **lvalue** reference." (Recall that a temporary can bind to a non-const **rvalue** reference.) – Cassio Neri Oct 17 '13 at 10:08