By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,443 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

Specializing Member Function Template of Class Template?

P: n/a
Hello!

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

template<typename Tclass thingy {
public:
template<typename UT f (const U &) const;
};

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

// The special case:-
template<typename Ttemplate<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

--
What happens if I mention Leader Kibo in my .signature?
Dec 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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 Tclass thingy {
public:

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

template<typename U>
typename boost::enable_if<
boost::is_same<T,U>,
T
>::type
f (const U &) const;

template<typename U>
typename boost::disable_if<
boost::is_same<T,U>,
T
>::type
f (const U &) const;
};
// The general case:-
template<typename T>
template<typename U>
typename boost::disable_if<
boost::is_same<T,U>,
T
>::type
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<T,U>,
T
>::type
thingy<T>::f
(const U &x) const {
std::cout << "special case\n";
return x;
}
int main()
{
thingy <intn;
n.f(1);
n.f(2.0);
}

regards
Andy Little

Dec 29 '06 #2

P: n/a
kwikius wrote:
>
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 Tclass thingy {
public:
template<typename UT 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 Uclass thingy_f_implementation {
public:
static T f (const U &x) {
::std::clog << "General." << ::std::endl;
return T(x);
}
};

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

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

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

int main () {
thingy<intx;
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

--
What happens if I mention Leader Kibo in my .signature?
Dec 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.