jeudi 3 septembre 2020

Prevent temporary instantiation of class

How can I prevent instantiation of temporary instances of a certain class?

I've tried creating a method which can be called upon lvalue instances only and calling that method in the c'tor to prevent rvalue instantiations of the class in compile-time, but it didn't help -- the method was called successfully even for rvalue instantiations of the class. It seems that the c'tor is oblivious to the fact that it's currently constructing an rvalue instance; lvalue-only method calls are allowed either way.

My goal is to create a scoped guard for allocations returned by WinApi, which must be freed via LocalFree. I want to prevent from temporary instances of the class, which would cause immediate deallocation of the allocation, defeating the purpose of being a scoped guard.

Aucun commentaire:

Enregistrer un commentaire