I would like to generate a compiler error if the using program calls a non-type template class method for a certain template argument.
typedef SubWithTemplate<1> SubWithTemplate1;
typedef SubWithTemplate<2> SubWithTemplate2;
SubWithTemplate1 &subWithTemplate1 = SubWithTemplate1::instance;
SubWithTemplate2 &subWithTemplate2 = SubWithTemplate2::instance;
subWithTemplate1.doSomething(); // Should compile OK
subWithTemplate1.doSomethingElse(); // Should compile OK
subWithTemplate2.doSomething(); // Should NOT compile OK
subWithTemplate2.doSomethingElse(); // Should compile OK
My starting point is the two following classes:
Super.h:
class Super {
protected:
Super() {}
public:
virtual void doSomething();
void doSomethingElse();
};
Super.cpp:
void Super::doSomething() {}
void Super::doSomethingElse() {}
SubWithTemplate.h:
template<int SUBNUMBER>
class SubWithTemplate : public Super {
public:
static SubWithTemplate<SUBNUMBER> instance;
void doSomething() {
// Do something
};
private:
SubWithTemplate() : Super() {}
};
template<int SUBNUMBER>
SubWithTemplate<SUBNUMBER> SubWithTemplate<SUBNUMBER>::instance;
I am not very fluent in Boost or mpl, but I have some vague feeling that BOOST_MPL_ASSERT could bring me some success. But I am not capable of understanding the nitty-gritty.
I tried something like:
SubWithTemplate.h:
...
void doSomething() {
BOOST_MPL_ASSERT_MSG(<some test on SUBNUMBER being different from 2 and 7 and less than 25>, <what here?>, <what here?> )
};
...
I do not want the Super to be templatized, as it should be the same instantiation for all subclasses.
If I could avoid the use of virtual on doSomething, even better.
I would be very thankful if some more-than-me-expert could help me.