 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Mon Aug 07, 2006 4:35 am Post subject: specialized constructor of templated class |
|
|
Hi there,
is this standard complaint code? My compiler (VC 7.1) gives me an error
(C2931). But, when searching the groups I could see that other compiler
swallow that.
template< class T >
class pixel_t
{
public:
pixel_t()
: _nRed ( 0 )
, _nGreen( 0 )
, _nBlue ( 0 )
{}
template< > pixel_t<float>()
: _nRed ( 0.f )
, _nGreen( 0.f )
, _nBlue ( 0.f )
{}
};
Thanks ahead,
Christian
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Mon Aug 07, 2006 8:24 am Post subject: Re: specialized constructor of templated class |
|
|
chhenning (AT) gmail (DOT) com wrote:
| Quote: | Hi there,
is this standard complaint code? My compiler (VC 7.1) gives me an error
(C2931). But, when searching the groups I could see that other compiler
swallow that.
template< class T
class pixel_t
{
public:
pixel_t()
: _nRed ( 0 )
, _nGreen( 0 )
, _nBlue ( 0 )
{}
template< > pixel_t<float>()
: _nRed ( 0.f )
, _nGreen( 0.f )
, _nBlue ( 0.f )
{}
};
|
VC 7.1 is correct: a member function of a template class cannot be
specialized within the general class template.
The solution is to move pixel_t<float>'s specialized constructor
outside of the pixel_t class template declaration:
template <>
pixel_t<float>::pixel_t() :
_nRed ( 0.f ),
_nGreen( 0.f ),
_nBlue ( 0.f )
{
...
}
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Mon Aug 07, 2006 8:27 am Post subject: Re: specialized constructor of templated class |
|
|
{ Please remove the banner from the quotation, unless relevant. -mod }
chhenning (AT) gmail (DOT) com wrote:
| Quote: | Hi there,
is this standard complaint code? My compiler (VC 7.1) gives me an error
(C2931). But, when searching the groups I could see that other compiler
swallow that.
template< class T
class pixel_t
{
public:
pixel_t()
: _nRed ( 0 )
, _nGreen( 0 )
, _nBlue ( 0 )
{}
template< > pixel_t<float>()
: _nRed ( 0.f )
, _nGreen( 0.f )
, _nBlue ( 0.f )
{}
};
Thanks ahead,
Christian
{ Banner stripped. -mod }
|
I cannot compile your above code under gcc4.0 and I can only definite a
specilaized constructor outside the class declaration like this:
template<> pixel<float>::pixel_t()
:_nRed(0.1f)
,_nGreen(0.1f)
,_nBlue(0.1f) {}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Hrayr BABAJANYAN Guest
|
Posted: Mon Aug 07, 2006 9:10 am Post subject: Re: specialized constructor of templated class |
|
|
Hi,
chhenning (AT) gmail (DOT) com wrote:
| Quote: | Hi there,
is this standard complaint code? My compiler (VC 7.1) gives me an error
(C2931). But, when searching the groups I could see that other compiler
swallow that.
template< class T
class pixel_t
{
public:
pixel_t()
: _nRed ( 0 )
, _nGreen( 0 )
, _nBlue ( 0 )
{}
template< > pixel_t<float>()
: _nRed ( 0.f )
, _nGreen( 0.f )
, _nBlue ( 0.f )
{}
};
Thanks ahead,
Christian
|
The "full" specialization of the member function of class template
should be declared/defined in the same namespace/scope as the class
declarationd (outside the class).
// >>> a.h
template<typename T>
class A
{
pubilc:
void f(T);
};
template<typename T>
void A::<T>f(T)
{}
template<> void A<int>::f(int); // move definition to some cpp file to
avoid liker errors...
// <<< a.h
// >>> a.cpp
template<>
void A<int>::f(int)
{
}
// <<< a.cpp
Cheers!
--
Hrayr
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|