C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

specialized constructor of templated class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon Aug 07, 2006 4:35 am    Post subject: specialized constructor of templated class Reply with 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


[ 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





PostPosted: Mon Aug 07, 2006 8:24 am    Post subject: Re: specialized constructor of templated class Reply with quote



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






PostPosted: Mon Aug 07, 2006 8:27 am    Post subject: Re: specialized constructor of templated class Reply with quote



{ 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





PostPosted: Mon Aug 07, 2006 9:10 am    Post subject: Re: specialized constructor of templated class Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.