Try this:
template <typename T, typename=void>
struct B : A
{
...
};
temlate <typename T>
struct B<T, typename std::enable_if<...some condition...>::type>:
A
{
virtual int f() const override { return 1; }
};
where we have two versions of B<T>
, one for which the condition is true (the enable_if
one), one for which the condition is false (the default one).
If you wanted to be able to reuse your default B
implementation, you could even do this:
template <typename T, typename=void>
struct B : A
{
...
};
template <typename T>
struct B<T, typename std::enable_if<...some condition...>::type>:
B<T, bool>
{
virtual int f() const override { return 1; }
};
where we inherit from the "false" case in the "true" case. But that is a bit dirty to me -- I'd rather put the common implementation in some third spot (B_impl
) rather than that hack. (That also lets you static assert that the second argument is void
in the first case of B
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…