Constructor problem

W

wij

Hi:

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
 
M

Marcel Müller

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() {};
};

Of course not. You told the compiler that you can use Level-1 but you
supplied an entirely different value.
How should I do to for such codes to compile correctly?

BigInt<1> a;
BigInt<5> b(a); // problem in constructing b

Whatever the meaning of Level is in your example. If you want to match
arbitrary numbers the signature of your constructor needs to be

template<unsigned Level2>
BigInt(const BigInt<Level2>& s);


Marcel
 
V

Victor Bazarov

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
 
S

SG

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

I guess you expected multiple user-defined conversions to happen here.
But only one is actually allowed to happen implicitly. What you need
is a templated constructor with a constraint:

template<unsigned Level2>
BigInt(BigInt<Level2> const& bi,
typename std::enable_if<
(Level2<Level)
::type* =0)
: m_lo(....
 
W

wij

(e-mail address removed)æ–¼ 2013å¹´3月14日星期四UTC+8下åˆ10時45分38秒寫é“:
Hi:



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

Many thanks folks.
At least now, I have some clue (enable_if). I'm trying hard to studying.
If sth can't work out, I should post again (hopefully in better description)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top