After it turned out that what I originally wanted is probably not possible w/o involving C++11 I want to slightly change the requirement and ask you if this can be achieved.
Basically I want to check in compile time if a class is inheriting from “interface”. By interface I mean class with pure virtual methods only.
I would like to do the following code:
template <typename T>
class Impl : public T {
public:
STATIC_ASSERT_INTERFACE(T);
};
The behavior here is if T has only pure virtual methods then it will compile and if one of its methods is not then fail.
Can anyone think of something like that?
This is basically similar to Java interfaces. In C++, there is no existence of
interfaceas such, it’s just a terminology used for aclasswith all pure-virtual methods and onlystatic constdata members.Additionally, pure virtual methods may or may not have a function body. Thus C++ pure virtual methods are not exactly same as Java’s abstract methods.
Unfortunately what you are asking is not possible to simulate in C++.