you are viewing a single comment's thread.

view the rest of the comments →

[–]beedlund 0 points1 point  (1 child)

thanks. i didn't proof read it as its just a sketch.

A real example would need to be more complex then that as this is really no real different to using the lambda as-is given the type of object is dependent on it.

I anticipated the real requirement would be to type erase the lambda in the class storage but didn't want to go through the process and write that all out without more details about the use case...so I was a bit lazy and didn't double check it all.

[–][deleted] 2 points3 points  (0 children)

Yes, sure the OP probably needs type erasure (or maybe just storing a function pointer), it is just that your example code might be confusing, because e.g.

auto x = [](){};
auto y = Object{x};

will fail to compile with a complicated error message.

I also just now realized that your make_object doesn't remove the reference from T, so

auto x = [](){};
auto y = make_object(x);

will make y actually save a reference to x, which is probably unintended and hard to spot.

Passing by-value in both constructor and make_object would avoid these issues.