Specializing Member Function Template of Class Template?

S

Simon G Best

Hello!

I'm trying to specialize a member function template of a class template,
like this:-

template<typename T> class thingy {
public:
template<typename U> T f (const U &) const;
};

// The general case:-
template<typename T> template<typename U> T thingy<T>::f
(const U &x) const {
return T(x);
}

// The special case:-
template<typename T> template<> T thingy<T>::f<T>
(const T &x) const {
return x;
}

But it's not working :-( I've tried a few variations (such as throwing
in a 'template' to disambiguate), but my compilers (G++ 4.1.2 and G++
3.3.6) just don't seem to like it. With the above C++, I'm getting the
following out of G++ 4.1.2:-

templates.cpp:13: error: invalid explicit specialization before ‘>’ token
templates.cpp:13: error: enclosing class templates are not explicitly
specialized
templates.cpp:14: error: template-id ‘f<T>’ for ‘T thingy<T>::f(const
T&) const’ does not match any template declaration
templates.cpp:14: error: invalid function declaration

:-(

What am I doing wrong?

Simon
 
K

kwikius

Simon G Best wrote:

What am I doing wrong?

Beacause the *parent* class is a template this is not allowed IIRC,
because someone could specialise the parent class which could cause
ambiguities. That is the rationale or something like it.

If you have boost you can try the enable_if technique --->

(Actually you could easily make your own versions of enable_if and
is_same if you dont have boost , but do put attribution to the
designers of course):

#include <boost/utility/enable_if.hpp>
//http://www.boost.org/libs/utility/enable_if.html
#include <boost/type_traits/is_same.hpp>
//http://www.boost.org/doc/html/boost_typetraits.html
#include <iostream>

Not sure if it works on older gcc though. Tested on VC7.1 and gcc4.1.

//--------------------------

template<typename T> class thingy {
public:

/*template <typename U>
T f( U const & x) const;*/

template<typename U>
typename boost::enable_if<
boost::is_same said:
f (const U &) const;

template<typename U>
typename boost::disable_if<
boost::is_same said:
f (const U &) const;
};


// The general case:-
template<typename T>
template<typename U>
typename boost::disable_if<
boost::is_same said:
thingy<T>::f (const U &x) const {

std::cout << "general case\n";
return T(x);
}
// special case
template<typename T>
template<typename U>
typename boost::enable_if<
boost::is_same said:
thingy<T>::f
(const U &x) const {
std::cout << "special case\n";
return x;
}


int main()
{
thingy <int> n;
n.f(1);
n.f(2.0);
}

regards
Andy Little
 
S

Simon G Best

kwikius said:
Beacause the *parent* class is a template this is not allowed IIRC,
because someone could specialise the parent class which could cause
ambiguities. That is the rationale or something like it.

If you have boost you can try the enable_if technique --->
[...]

Thanks, but I don't think changing the class template itself is
justified, as my intended specialization isn't part of the interface,
but only for dealing with implementation details.

What I could do, though, is something like the following:-

[Start C++ here.]
#include <iostream>

template<typename T> class thingy {
public:
template<typename U> T f (const U &) const;
};

namespace {

/* Here's how I can get the kind of specialization I'm after.
*/

// The general case:-
template<typename T, typename U> class thingy_f_implementation {
public:
static T f (const U &x) {
::std::clog << "General." << ::std::endl;
return T(x);
}
};

// The special case:-
template<typename T> class thingy_f_implementation<T, T> {
public:
static T f (const T &x) {
::std::clog << "Special." << ::std::endl;
return x;
}
};
}

template<typename T> template<typename U> T thingy<T>::f
(const U &x) const {
return thingy_f_implementation<T, U>::f(x);
}

/* And just to prove it works:-
*/

int main () {
thingy<int> x;
x.f(float(2.3));
x.f(int(42));
return 0;
}
[End C++ here.]

But I'd rather do it more directly, as indicated in my original post. I
hoped it was just that I'd messed up the syntax, or something. But if,
as you say, it just can't be done that way, then I suppose taking the
scenic route (as above) would have to be it.

But thanks for your suggestion anyway :)

Simon
 

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,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top