On 5/26/2015 3:43 PM, Stefan Ram wrote:
> I am not an expert for this topic, here is a wild guess:
Truly wild...
>
> #include <iostream>
> #include <ostream>
>
> class A;
> class B;
>
> template< typename T >struct common {};
> template< typename T >struct C : common< T >{ const int m_code; };
> template<> struct C< A >: common< A >{ const int c = 5; };
> template<> struct C< B >: common< B >{ const int c = 10; };
>
> int main(){ ::std::cout << C< A >{}.c << '\n'; }
There is no need to use inheritance, IMHO. You need some kind of type
traits stuff, specialized for A and B, and use it in Foo:
class A; // some non trivial class.
class B; // another non trivial class.
template<typename T> struct traits_used_by_Foo {
};
template<> struct traits_used_by_Foo<A> {
enum { code = 5 };
};
template<> struct traits_used_by_Foo<B> {
enum { code = 10 };
};
template<typename T> class Foo
{
public:
// some public stuff usint T.
private:
const int m_code = traits_used_by_Foo<T>::code;
// some private stuff which uses T.
};
V
--
I do not respond to top-posted replies, please don't ask