Search ProofOfProgress Blog

Monday, February 29, 2016

Enforce classes have same static method c++

All my classes must have a method called unitTest in DEBUG mode. This is because I don't want my test code in a separate library.

Separation of concerns is nice and all. But I think that separation also makes it more likely that your test library code will decay.

This might be what I need: http://stackoverflow.com/questions/9346076/static-template-functions-in-a-class

Basically, extend from a header-only class that defines no implementation. And have the function within that class be templated.

Something like:

public:
template< typename T>
    static double foo( vector arr );

In the header, without any implementation.

Can you extend from a class with no implementation? I bet you can. Need to look into that.

Maybe I need a "pure virtual function"? https://blogs.msdn.microsoft.com/oldnewthing/20131011-00/?p=2953

But that specifies type... Do I need a "pure virtual template function"? That sounds fancy and over complicated.

Yes, you can. http://stackoverflow.com/questions/8919566/are-pure-virtual-methods-allowed-within-a-template-class

No comments:

Post a Comment