Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructor problem

Reply
Thread Tools

Constructor problem

 
 
wij@seed.net.tw
Guest
Posts: n/a
 
      03-14-2013
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

 
Reply With Quote
 
 
 
 
Marcel Mller
Guest
Posts: n/a
 
      03-14-2013
On 14.03.2013 15:45, 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() {};
> };


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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-14-2013
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
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      03-14-2013
On Mar 14, 3:45*pm, w...@seed.net.tw 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


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(....
 
Reply With Quote
 
wij@seed.net.tw
Guest
Posts: n/a
 
      03-15-2013
w...@seed.net.tw於 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments