On 3/14/2013 10:45 AM,
wrote:
> In the class BigInt, the 3rd ctor won't work as desired (accepting lower
> level classes).
>
> template<unsigned int Level>
> class BigInt {
> int m_lo,m_hi;
> public:
> BigInt() : m_lo(),m_hi() {};
> BigInt(const BigInt& s) : m_lo(s.m_lo),m_hi(s.m_hi) {};
> BigInt(const BigInt<Level-1>& s) : m_lo(s),m_hi() {};
> };
>
> How should I do to for such codes to compile correctly?
>
> BigInt<1> a;
> BigInt<5> b(a); // problem in constructing b
The "3rd c-tor" for BigInt<5> class is made to only accept BigInt<4> by
means of providing the explicit template argument 'Level-1'. It does
not accept any other type, and BigInt<1> is not convertible to BigInt<4>.
Marcel hinted at what you might want to do to accept BigInt<1> to
construct a BigInt<5>, but that's not exactly what you seem to want,
either. You seem to want only to accept the instantiations of BigInt
template with Level smaller than this one you're constructing. It's not
that simple. You probably want to add a hidden argument to the template
constructor (see Marcel Mueller's post) that would ensure that the
Level2 is actually smaller than Level, and would prevent the compiler's
generation of the template it the relationship is not what you want (see
SFINAE). That would lead to a compilation error in case like
BigInt<2> c(b);
but that's OK, I gather.
V
--
I do not respond to top-posted replies, please don't ask