implementation: specialization of member of template class

V

vlad.k.sm

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.
 
V

vlad.k.sm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top