473,756 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specializing Member Function Template of Class Template?

Hello!

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

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

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

// The special case:-
template<typena me 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:1 3: error: invalid explicit specialization before ‘>’ token
templates.cpp:1 3: error: enclosing class templates are not explicitly
specialized
templates.cpp:1 4: error: template-id ‘f<T>’ for ‘T thingy<T>::f(co nst
T&) const’ does not match any template declaration
templates.cpp:1 4: 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 2065

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_typetrait s.html
#include <iostream>

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

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

template<typena me Tclass thingy {
public:

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

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

template<typena me U>
typename boost::disable_ if<
boost::is_same< T,U>,
T
>::type
f (const U &) const;
};
// The general case:-
template<typena me T>
template<typena me 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<typena me T>
template<typena me U>
typename boost::enable_i f<
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<typena me Tclass thingy {
public:
template<typena me UT f (const U &) const;
};

namespace {

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

// The general case:-
template<typena me T, typename Uclass thingy_f_implem entation {
public:
static T f (const U &x) {
::std::clog << "General." << ::std::endl;
return T(x);
}
};

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

template<typena me Ttemplate<typen ame UT thingy<T>::f
(const U &x) const {
return thingy_f_implem entation<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
5778
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
3
2125
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 template arguments Line 29: 'bar' : unable to match function definition to an existing declaration What am I doing wrong?
2
1504
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 of the identity function as follows, template <class T> T identity(T x) { return x; } and for some reason I want to specialize this function for the
9
2762
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 following short program duplicates this error. Compile it in Debug mode (all optimizations disabled), with "Not using precompiled headers". Class C defines F as a template member function taking (_T const &). It then specializes F taking a char....
4
4065
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 #ifndef TEST_HEADER_H #define TEST_HEADER_H
3
2921
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 T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
8
2437
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
7698
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
8381
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.