Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Specializing template member function

Reply
Thread Tools

Specializing template member function

 
 
Igor R.
Guest
Posts: n/a
 
      11-10-2009
Is it permitted in Standard to specialize template member function of
a non-specialized class template? If yes, what's the correct syntax?

template<class T> struct Test
{
template<class U> void f()
{}
// MSVC permits specializations inside the class scope, but it's
seems to be non-standard:
template<> void f<int>()
{}
};

// doesn't compile both with MSVC and gcc:
template<class T>
template<>
Test::f<char>()
{}

Thanks.
 
Reply With Quote
 
 
 
 
Igor R.
Guest
Posts: n/a
 
      11-10-2009
> // doesn't compile both with MSVC and gcc:
> template<class T>
> template<>
> Test::f<char>()
> {}


I mean:
template<class T>
template<>
Test<T>::f<char>()
{}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-10-2009
Igor R. wrote:
> Is it permitted in Standard to specialize template member function of
> a non-specialized class template? If yes, what's the correct syntax?


No.

Consider overloading.

> template<class T> struct Test
> {
> template<class U> void f()
> {}
> // MSVC permits specializations inside the class scope, but it's
> seems to be non-standard:
> template<> void f<int>()
> {}


Yes, this is non-standard.

> };
>
> // doesn't compile both with MSVC and gcc:
> template<class T>
> template<>
> Test::f<char>()
> {}
>
> Thanks.


Sometimes it's possible to overload the function, but you need arguments
to differentiate it from the template. You might give it a fake one...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Vladimir Jovic
Guest
Posts: n/a
 
      11-11-2009
Victor Bazarov wrote:
> Igor R. wrote:
>> Is it permitted in Standard to specialize template member function of
>> a non-specialized class template? If yes, what's the correct syntax?

>
> No.
>
> Consider overloading.
>
>> template<class T> struct Test
>> {
>> template<class U> void f()
>> {}
>> // MSVC permits specializations inside the class scope, but it's
>> seems to be non-standard:
>> template<> void f<int>()
>> {}

>
> Yes, this is non-standard.
>
>> };
>>
>> // doesn't compile both with MSVC and gcc:
>> template<class T>
>> template<>
>> Test::f<char>()
>> {}
>>
>> Thanks.

>
> Sometimes it's possible to overload the function, but you need arguments
> to differentiate it from the template. You might give it a fake one...
>


He probably meant something like this:

#include <iostream>
template< typename T >
class A
{
public:
A();
};
template < typename T >
A< T >::A()
{
std::cout<<"T"<<std::endl;
}
template <>
A< int >::A()
{
std::cout<<"int"<<std::endl;
}
int main()
{
A< int > a1;
A< double > a2;
}



--
Bolje je ziveti sto godina kao bogatun, nego jedan dan kao siromah!
 
Reply With Quote
 
Igor R.
Guest
Posts: n/a
 
      11-11-2009
Victor Bazarov,

> Sometimes it's possible to overload the function, but you need arguments to differentiate it from the template. You might give it a fake one...


Ok, I'll go this way. Thanks!


Vladimir Jovic,

> He probably meant something like this:


No, I didn't.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there aA correct way of specializing member functions of template classes? m0shbear C++ 2 03-01-2011 09:48 AM
partially specializing member functions of a template class Rahul C++ 8 07-16-2007 02:22 PM
Specializing Member Function Template of Class Template? Simon G Best C++ 2 12-29-2006 12:32 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Trouble specializing a member function in a template class Jeff C++ 2 11-20-2003 04:39 AM



Advertisments