Constructor problem

Discussion in 'C++' started by wij@seed.net.tw, Mar 14, 2013.

  1. Guest

    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
     
    , Mar 14, 2013
    #1
    1. Advertising

  2. 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
     
    Marcel Müller, Mar 14, 2013
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Mar 14, 2013
    #3
  4. SG Guest

    On Mar 14, 3:45 pm, 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(....
     
    SG, Mar 14, 2013
    #4
  5. Guest

    æ–¼ 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)
     
    , Mar 15, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,068
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,352
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,875
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    489
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,349
Loading...

Share This Page