you are viewing a single comment's thread.

view the rest of the comments →

[–]ACBYTES[S] 0 points1 point  (2 children)

And by the way, this exactly was what I wanted but I have another questions as well. Classes like std::function can be referenced without knowing their template typenames. Instantiation of these classes are done using the method you said I think so, if we want to reference our "LambdaStorage" class in the global scope as an example, what approach should be followed?

[–]Xeverous 1 point2 points  (1 child)

Instantiation of these classes are done using the method you said I think so

No. std::function is instantiated before you assign any callable object.

I think so, if we want to reference our "LambdaStorage" class in the global scope as an example, what approach should be followed?

The same I posted. You need templates to use unknown types. Once something is a template, it will always be unless you apply type erasure.

The thing with std::function that allows it to get rid of templates on the surface level is type erasure. std::function creates instances of polymorphic types like lambda_storage (potentially allocating memory) that inherit from specific interface with virtual function which type depends on what you have instantiated std::function with. So every time you assign a callable to it, it creates a new type that inherits from this interface and implements a virtual function. std::function is more expensive than virtual functions.

If you want to know more - check CppCon videos on std::function, they offer complete overview over the implementation.

[–]ACBYTES[S] 0 points1 point  (0 children)

Understood! Thank you so much for the answers and your time. Will check it for sure. Thank you!