473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Specializing Member Function Template of Class Template?

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
2 2043

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
3
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit...
2
by: Alex Drummond | last post by:
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation...
9
by: Eric Wang | last post by:
VC 7.1.3088 Bug: When a class defines a template member function taking (T const &), then an explicit specialization taking a non-const & argument (U) causes a C1001 at msc1.cpp line 2701. The...
4
by: James Aguilar | last post by:
Guys, When I specialize a template class member function (I.e. a member function of a template class) based on that class' type, bad things happen. Here's some code: ---- test_header.h...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.