Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > implementation: specialization of member of template class

Reply
Thread Tools

implementation: specialization of member of template class

 
 
vlad.k.sm@gmail.com
Guest
Posts: n/a
 
      03-03-2009
Specialization of member of template class is impossible according to C
++ standard. The following code:

template<typename T> struct enbl_if {};
template<> struct enbl_if<int> { typedef int type; };

template<typename T> struct disble_if {typedef T type; };
template<> struct disble_if<int> {};

template<typename T>
struct test
{
template<typename T2>
typename enbl_if<T2>::type get() const
{
std::cout << "enbl_if" << std::endl;
return T2();
}

template<typename T2>
typename disble_if<T2>::type get() const
{
std::cout << "disble_if" << std::endl;
return T2();
}
};


int main()
{
int i = test<int>().get<int>();
long l = test<int>().get<long>();
std::string s = test<int>().get<std::string>();
return 0;
}

prints:
enbl_if
disble_if
disble_if

and so, it works as full specialization for "int". I was able to
compile it under gcc 4.2, VC2003-2005, comeua online. Is it correct
code?
p.s.: instead enbl_if, disble_if need to use appropriate boost utility
in complete code.
 
Reply With Quote
 
 
 
 
vlad.k.sm@gmail.com
Guest
Posts: n/a
 
      03-05-2009
On Mar 3, 10:55*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Specialization of member of template class is impossible according to C
> > ++ standard. The following code:

>
> > template<typename T> struct enbl_if {};
> > template<> struct enbl_if<int> { typedef int type; };

>
> > template<typename T> struct disble_if {typedef T type; };
> > template<> struct disble_if<int> {};

>
> > template<typename T>
> > struct test
> > {
> > * *template<typename T2>
> > * *typename enbl_if<T2>::type get() const
> > * *{
> > * * * std::cout << "enbl_if" << std::endl;
> > * * * return T2();
> > * *}

>
> > * *template<typename T2>
> > * *typename disble_if<T2>::type get() const
> > * *{
> > * * * std::cout << "disble_if" << std::endl;
> > * * * return T2();
> > * *}
> > };

>
> > int main()
> > {
> > * *int i = test<int>().get<int>();
> > * *long l = test<int>().get<long>();
> > * *std::string s = test<int>().get<std::string>();
> > * *return 0;
> > }

>
> > prints:
> > enbl_if
> > disble_if
> > disble_if

>
> > and so, it works as full specialization for "int". I was able to
> > compile it under gcc 4.2, VC2003-2005, comeua online. Is it correct
> > code?

>
> Seems OK. *Why are you concerned? *It would seem that you heard that
> "specialisation is not allowed" but are not sure whether it applies
> here. *Actually, what's not allowed is to declare a specialisation of a
> template member without specialising the template. *You're not declaring
> any specialisations here.
>

I am concerned because this isn't a valid overloading if remove
enable_if/disable_if.
 
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
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
template member specialization of template class toton C++ 2 02-22-2008 12:28 PM
Specialization of member function template in template class? Joseph Turian C++ 4 03-20-2006 10:07 AM
Out-of-class-template member template specialization syntax BigMan C++ 1 03-26-2005 02:23 PM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments