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

Home Posts Topics Members FAQ

Full specialization of a member function template of a class template


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?

Thanks,
Dave
#include <iostream>

using namespace std;

template <typename T1>
class foo
{
public:
template <typename T2>
void bar(const T2 &param);
};

template <typename T1>
template <typename T2>
void foo<T1>::bar(co nst T2 &param)
{
static_cast<voi d>(param);

cout << "Point 1" << endl;
}

template <typename T1>
template <>
void foo<T1>::bar<do uble>(const double &param)
{
static_cast<voi d>(param);

cout << "Point 2" << endl;
}

int main()
{
foo<int> var;

var.bar(4.5);
}
Jul 22 '05 #1
3 2125
"Dave" <be***********@ yahoo.com> wrote...

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?
It is expressly prohibited. If you want to explicitly specialise
a member template, the enclosing class has to be specialised as well.

See 14.7.3/17.

Thanks,
Dave
#include <iostream>

using namespace std;

template <typename T1>
class foo
{
public:
template <typename T2>
void bar(const T2 &param);
};

template <typename T1>
template <typename T2>
void foo<T1>::bar(co nst T2 &param)
{
static_cast<voi d>(param);

cout << "Point 1" << endl;
}

template <typename T1>
template <>
void foo<T1>::bar<do uble>(const double &param)
{
static_cast<voi d>(param);

cout << "Point 2" << endl;
}

int main()
{
foo<int> var;

var.bar(4.5);
}

Jul 22 '05 #2
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:UfXlc.2499 4$_41.1789783@a ttbi_s02
"Dave" <be***********@ yahoo.com> wrote...

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?


It is expressly prohibited. If you want to explicitly specialise
a member template, the enclosing class has to be specialised as well.

See 14.7.3/17.


In this case a workaround is to add the specialised member function to the
class declaration:

template <typename T1>
class foo
{
public:
template <typename T2>
void bar(const T2 &param);

void bar(const double &param);
};

template <typename T1>
void foo<T1>::bar(co nst double &param)
{
static_cast<voi d>(param);
cout << "Point 2" << endl;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3
Dave wrote:
Hello all,

I am trying to create a full specialization of a member function template of
a class template.
From the standard (towards the end of 14.7.3),

"In an explicit specialization declaration for a member of a class
template or a member template that appears in namespace scope, the
member template and some of its enclosing class templates may remain
unspecialized, except that the declaration shall not explicitly
specialize a class member template if its enclosing class templates
are not explicitly specialized as well."

Crystal clear?

You can't explicitly specialize a member template of a class template,
but you can explicitly specialize a member template of an explicit
specialization 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


That's pretty crap. G++ 3.3.1 gives "enclosing class templates are not
explicitly specialized".

--
Regards,
Buster.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
6861
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
6
1624
by: Kalle Rutanen | last post by:
Here is a short code. I can't see why the template specialization does not work (vsnet2003)? Does the standard allow these kind of specializations? template <int i> class B {}; template <typename T> class A {
4
3336
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
2
2317
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know you get do something like the following, just want to know how it works with the overloading of get<>(). boost::tupple<int,doubletup(1,2.0); double d=tup.get<2>(); // equal 2.0 // simple set up,
9
3470
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
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:
13
6597
by: mike b | last post by:
Hello everyone, thanks in advance for your help. I'm new to C++ templates and have run into some issues using member function templates. I have a shared library containing templates that I'm trying to use from an executable, compile using gcc 4.1.2. Everything works fine until I try specializing one of the static member function templates in a non-template class. I have a feeling I'm messing up something obvious so before I post a...
1
1438
by: Nick Bastin | last post by:
I've done some searching in this newsgroup and on the internet and haven't found an answer, so I thought I'd ask here... :-) I don't understand why full specialization of a member function template in a class template is only valid if you fully specialize the class template as well. Consider the following example: template<class T, int ID> class RSMixin : public T
10
2790
by: jason.cipriani | last post by:
I never seem to be able to get this right. Here I have some code: template <typename T, int Nclass A { void f (T); }; template <typename Tvoid A<T,1>::f (T) { } template <typename Tvoid A<T,3>::f (T) {
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
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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

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.