Making operator

The problem with this setup is that the operator<< you defined above is a free function, which can’t be virtual (it has no receiver object). In order to make the function virtual, it must be defined as a member of some class, which is problematic here because if you define operator<< as a member of … Read more

C++ Virtual template method

The problem is that you cannot mix static time polymorphism (templates) with runtime polymorphism easily. The reason for the language disallowing the particular construct in your example is that there are potentially infinite different types that could be instantiating your template member function, and that in turn means that the compiler would have to generate … Read more

How to do virtual file processing?

You might want to consider using a tempfile.SpooledTemporaryFile which gives you the best of both worlds in the sense that it will create a temporary memory-based virtual file initially but will automatically switch to a physical disk-based file if the data held in memory exceeds a specified size. Another nice feature is that (when using … Read more

CRTP to avoid dynamic polymorphism

There are two ways. The first one is by specifying the interface statically for the structure of types: template <class Derived> struct base { void foo() { static_cast<Derived *>(this)->foo(); }; }; struct my_type : base<my_type> { void foo(); // required to compile. }; struct your_type : base<your_type> { void foo(); // required to compile. }; … Read more

C++ static virtual members?

No, there’s no way to do it, since what would happen when you called Object::GetTypeInformation()? It can’t know which derived class version to call since there’s no object associated with it. You’ll have to make it a non-static virtual function to work properly; if you also want to be able to call a specific derived … Read more