explicit specialization od c'tor

Discussion in 'C++' started by Gernot Frisch, Jan 17, 2005.

  1. // variable creation object
    template <class T> class CL
    {
    public:
    CL (const T& t) {}
    template <> CL<double>(const T& t) {}
    };

    How do I make a constructor for T = double, ..., so I can distinguish
    between the types.

    Thank you,

    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
     
    Gernot Frisch, Jan 17, 2005
    #1
    1. Advertising

  2. "Gernot Frisch" <> skrev i en meddelelse
    news:...
    > // variable creation object
    > template <class T> class CL
    > {
    > public:
    > CL (const T& t) {}
    > template <> CL<double>(const T& t) {}
    > };
    >
    > How do I make a constructor for T = double, ..., so I can distinguish
    > between the types.
    >

    Simply have the constructor untemplated:

    template <class T> class CL
    {
    public:
    CL (const T& t) {}
    CL(double t) {}
    };

    /Peter

    > Thank you,
    >
    > --
    > -Gernot
    > int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",
    > "ma", 58, 'g', 64, "ba", 46, 10);}
    >
    > ________________________________________
    > Looking for a good game? Do it yourself!
    > GLBasic - you can do
    > www.GLBasic.com
    >
    >
     
    Peter Koch Larsen, Jan 17, 2005
    #2
    1. Advertising

  3. Peter Koch Larsen wrote:

    > Simply have the constructor untemplated:
    >
    > template <class T> class CL
    > {
    > public:
    > CL (const T& t) {}
    > CL(double t) {}
    > };



    This does not work.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 17, 2005
    #3
  4. Gernot Frisch wrote:

    > // variable creation object
    > template <class T> class CL
    > {
    > public:
    > CL (const T& t) {}
    > template <> CL<double>(const T& t) {}
    > };
    >
    > How do I make a constructor for T = double, ..., so I can distinguish
    > between the types.



    I cannot think of another way...


    template <>
    class CL<double>
    {
    public:
    CL (const double& t) {}
    };




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 17, 2005
    #4
  5. Gernot Frisch wrote:
    > // variable creation object
    > template <class T> class CL
    > {
    > public:
    > CL (const T& t) {}
    > template <> CL<double>(const T& t) {}
    > };
    >
    > How do I make a constructor for T = double, ..., so I can distinguish
    > between the types.
    > ...


    If you want to provide the explicit specialization for the constructor
    alone you simply define it as follows

    template<class T> class CL
    {
    CL(const T& t) {}
    };

    template<> CL<double>::CL(const double& t)
    {
    // Specialized implementation goes here
    }

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jan 17, 2005
    #5
  6. Andrey Tarasevich wrote:

    > If you want to provide the explicit specialization for the constructor
    > alone you simply define it as follows
    >
    > template<class T> class CL
    > {


    public:

    > CL(const T& t) {}
    > };
    >
    > template<> CL<double>::CL(const double& t)
    > {
    > // Specialized implementation goes here
    > }



    Pretty cool. How can we provide the declaration in this style?




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 17, 2005
    #6
  7. Ioannis Vranos wrote:
    > Andrey Tarasevich wrote:
    >
    >> If you want to provide the explicit specialization for the constructor
    >> alone you simply define it as follows
    >>
    >> template<class T> class CL
    >> {

    >
    >
    > public:
    >
    >> CL(const T& t) {}
    >> };
    >>
    >> template<> CL<double>::CL(const double& t)
    >> {
    >> // Specialized implementation goes here
    >> }

    >
    >
    >
    > Pretty cool. How can we provide the declaration in this style?


    What for? The original template definition already contains a declaration
    anybody would need...
     
    Victor Bazarov, Jan 17, 2005
    #7
  8. Ioannis Vranos wrote:
    >
    > Pretty cool. How can we provide the declaration in this style?



    I just noticed that this

    template<class T> class CL
    {
    public:
    CL(const T& t) {}
    };


    // Declaration
    template<>
    CL<double>::CL(const double& t);


    template<>
    CL<double>::CL(const double& t)
    {
    // Specialized implementation goes here
    }


    int main()
    {
    CL<double> a(2);
    }


    compiles.



    Pretty weird style.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 17, 2005
    #8
  9. Victor Bazarov wrote:

    > What for? The original template definition already contains a declaration
    > anybody would need...



    For whatever reason (export may be? - or something else). :)



    Anyway as I say in another message I just sent, probably I found the
    declaration style. Pretty weird stuff.


    I am not sure TC++PL mentions this style, or I have forgotten.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 17, 2005
    #9
  10. Ioannis Vranos wrote:
    > Victor Bazarov wrote:
    >
    >> What for? The original template definition already contains a
    >> declaration
    >> anybody would need...

    >
    >
    >
    > For whatever reason (export may be? - or something else). :)
    >
    >
    >
    > Anyway as I say in another message I just sent, probably I found the
    > declaration style. Pretty weird stuff.
    >
    >
    > I am not sure TC++PL mentions this style, or I have forgotten.


    You can think of every function _definition_ as consisting of
    a declaration and a body. So, if you already have a definition, just
    replace the body with a semicolon :)
     
    Victor Bazarov, Jan 17, 2005
    #10
  11. Victor Bazarov wrote:

    > You can think of every function _definition_ as consisting of
    > a declaration and a body. So, if you already have a definition, just
    > replace the body with a semicolon :)



    Yes that's what I thought. However it is the first time I see a
    constructor definition (and declaration!) that is valid outside of the
    class body and invalid in the inside.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Jan 17, 2005
    #11
  12. Ioannis Vranos wrote:
    > ...
    >> You can think of every function _definition_ as consisting of
    >> a declaration and a body. So, if you already have a definition, just
    >> replace the body with a semicolon :)

    >
    > Yes that's what I thought. However it is the first time I see a
    > constructor definition (and declaration!) that is valid outside of the
    > class body and invalid in the inside.
    > ...


    It makes sense to me. This member specialization is only relevant to one
    concrete specialization of the class template (the one with 'T ==
    double'), not to the entire class template. It would be strange to see
    it inside.

    Now, if the constructor itself was a template

    template<class T> class CL
    {
    public:
    template<class U> CL(const U& t) {}
    ...
    };

    then it would be logical to allow declaring/defining it's
    specializations inside the class definition

    template<class T> class CL
    {
    public:
    template<class U> CL(const U& t) {}

    template<> CL(const double& t) { /* ... */ }
    template<> CL(const int& t) { /* ... */ }
    ...
    };

    The above is only provided as a syntactical sketch. The latter code is
    still illegal in C++ for a completely unrelated reason: member templates
    cannot be explicitly specialized without explicit specialization of the
    enclosing class template (come to think of it, I still don't know the
    rationale for this limitation).

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jan 17, 2005
    #12
    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. Andriy Shnyr
    Replies:
    3
    Views:
    760
    Larry Evans
    Dec 4, 2003
  2. J.T. Conklin
    Replies:
    1
    Views:
    443
    David Hilsee
    Aug 11, 2004
  3. Patrick Kowalzick
    Replies:
    0
    Views:
    819
    Patrick Kowalzick
    Oct 29, 2004
  4. Andy
    Replies:
    5
    Views:
    508
    Shezan Baig
    Jan 30, 2005
  5. Replies:
    1
    Views:
    580
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page