Implicit conversion constructor with template classes

Discussion in 'C++' started by flopbucket, Jun 25, 2006.

  1. flopbucket

    flopbucket Guest

    Hi,

    If I have:

    template<class T>
    class X
    {
    .....
    };

    then:

    template<class U>
    class Y
    {
    public:
    typedef X<U> NormalType;
    typedef X<const U> ConstType;

    NormalType foo() { return NormalType(); }
    };

    How can I (or can I?) have conversion from NormalType to ConstType so
    the following would work:

    Y<int> bar;
    ConstType i = bar.foo();

    Can I have a constructor in Y like "Y(const Y<const T>&)" to provide
    implicit conversion? If I do something in Y like:

    template<class Z>
    Y(const Z&)

    That would apply to any type and try to convert, wouldnt it?

    Is there a way I can specify I can accept a type "const T" when the
    template is instantiated with just T?


    Thanks for any tips.
     
    flopbucket, Jun 25, 2006
    #1
    1. Advertising

  2. flopbucket

    Tom Widmer Guest

    flopbucket wrote:
    > Hi,
    >
    > If I have:
    >
    > template<class T>
    > class X
    > {
    > ....
    > };
    >
    > then:
    >
    > template<class U>
    > class Y
    > {
    > public:
    > typedef X<U> NormalType;
    > typedef X<const U> ConstType;
    >
    > NormalType foo() { return NormalType(); }
    > };
    >
    > How can I (or can I?) have conversion from NormalType to ConstType so
    > the following would work:
    >
    > Y<int> bar;
    > ConstType i = bar.foo();


    Presumably you can modify Y's code. Can you modify X's too?

    > Can I have a constructor in Y like "Y(const Y<const T>&)" to provide
    > implicit conversion? If I do something in Y like:
    >
    > template<class Z>
    > Y(const Z&)
    >
    > That would apply to any type and try to convert, wouldnt it?


    Yes, but it would convert it to a Y, not an X.

    > Is there a way I can specify I can accept a type "const T" when the
    > template is instantiated with just T?


    You have to be careful, since the type obviously needs to work with
    const. In your case, something like this should work:

    template <class T>
    class X
    {
    public:
    X(X<const T> const&); //construct from const.
    };

    template <class T>
    class X<const T>
    {
    //no const T constructor
    };

    You could move common functionality into a base class to avoid excessive
    repetition.

    Tom
     
    Tom Widmer, Jun 26, 2006
    #2
    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. sb
    Replies:
    10
    Views:
    4,611
    Andre Heinen
    Feb 25, 2004
  2. Alex Vinokur
    Replies:
    9
    Views:
    413
    Thomas J. Gritzan
    Aug 10, 2006
  3. Jess
    Replies:
    3
    Views:
    301
    Robert Bauck Hamar
    Jun 14, 2007
  4. alexroat
    Replies:
    1
    Views:
    298
    Alf P. Steinbach
    Jul 24, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,248
Loading...

Share This Page