469,898 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

explicit specialization od c'tor

// 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
Jul 22 '05 #1
11 1686

"Gernot Frisch" <Me@Privacy.net> skrev i en meddelelse
news:35*************@individual.net...
// 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

Jul 22 '05 #2
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
Jul 22 '05 #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
Jul 22 '05 #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.
...


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
Jul 22 '05 #5
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
Jul 22 '05 #6
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...
Jul 22 '05 #7
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
Jul 22 '05 #8
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
Jul 22 '05 #9
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 :-)
Jul 22 '05 #10
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
Jul 22 '05 #11
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
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Marcelo Pinto | last post: by
8 posts views Thread by trying_to_learn | last post: by
1 post views Thread by Thomas Barnet-Lamb | last post: by
reply views Thread by greek_bill | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.