| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Gernot Frisch" |
| Date: |
17 Jan 2005 04:26:14 AM |
| Object: |
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
.
|
|
| User: "Peter Koch Larsen" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 04:46:41 AM |
|
|
"Gernot Frisch" <Me@Privacy.net> skrev i en meddelelse
news:351i2fF4g2o49U1@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
.
|
|
|
| User: "Ioannis Vranos" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 06:55:31 AM |
|
|
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
.
|
|
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 01:12:07 PM |
|
|
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
.
|
|
|
| User: "Ioannis Vranos" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 02:39:36 PM |
|
|
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
.
|
|
|
| User: "Ioannis Vranos" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 03:01:28 PM |
|
|
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
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 02:58:57 PM |
|
|
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...
.
|
|
|
| User: "Ioannis Vranos" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 03:04:07 PM |
|
|
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
.
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 03:24:41 PM |
|
|
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 :-)
.
|
|
|
| User: "Ioannis Vranos" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 03:27:14 PM |
|
|
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
.
|
|
|
| User: "Andrey Tarasevich" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 05:20:53 PM |
|
|
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
.
|
|
|
|
|
|
|
|
|
|
| User: "Ioannis Vranos" |
|
| Title: Re: explicit specialization od c'tor |
17 Jan 2005 06:56:20 AM |
|
|
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
.
|
|
|
|

|
Related Articles |
|
|