473,394 Members | 2,090 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,394 software developers and data experts.

Template specializations

Hi,

this code

#include <iostream>

template< typename T >
struct X {
void f();
};

template<> void X<long>::f() {std::cout << "X<long>::f()\n";}

int main(int, char**)
{
X<long> xi;
xi.f();
return 0;
}

is accepted by GCC4, Comeau Online, and VC7.1.
I thought one cannot specialize only a member of a
class template, but has to specialize the whole
template. It seems that this isn't true?

TIA,

Hendrik Schober

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
Sep 21 '05 #1
9 1575

"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:43*********************@news.sunsite.dk...
Hi,

this code

#include <iostream>

template< typename T >
struct X {
void f();
};

template<> void X<long>::f() {std::cout << "X<long>::f()\n";}

int main(int, char**)
{
X<long> xi;
xi.f();
return 0;
}

is accepted by GCC4, Comeau Online, and VC7.1.
I thought one cannot specialize only a member of a
class template, but has to specialize the whole
template. It seems that this isn't true?

TIA,

Hendrik Schober


Yes, you can specialize non-template, static data members and member
functions of a class template. However, only full specialization is
supported.

Ben
Sep 21 '05 #2
"Hendrik Schober" <Sp******@gmx.de> wrote in message
news:43*********************@news.sunsite.dk
Hi,

this code

#include <iostream>

template< typename T >
struct X {
void f();
};

template<> void X<long>::f() {std::cout << "X<long>::f()\n";}

int main(int, char**)
{
X<long> xi;
xi.f();
return 0;
}

is accepted by GCC4, Comeau Online, and VC7.1.
I thought one cannot specialize only a member of a
class template, but has to specialize the whole
template. It seems that this isn't true?


Correct. From Vandevoorde and Josuttis, C++ Templates, p.190:

"Class templates and function templates can be fully specialized. So can
members of class templates that may be defined outside the body of a class
definition (i.e., member functions, nested classes, and static data
members)." Doubtless, you can find it in the standard as well.
--
John Carson

Sep 21 '05 #3
And btw the following code shows how encapsulation is lost by providing a
member function specialization:

#include <iostream>

template <typename T>
class A
{
private: T t;
public: void f(void){}
};

template<>
void A<int>::f(void)
{
t++; // access private member
std::cout << "t tempered\n";
}

int main(void)
{
A<int> a;
a.f();
}
Sep 21 '05 #4

benben wrote:
And btw the following code shows how encapsulation is lost by providing a
member function specialization:

#include <iostream>

template <typename T>
class A
{
private: T t;
public: void f(void){}
};

template<>
void A<int>::f(void)
{
t++; // access private member
std::cout << "t tempered\n";
}

int main(void)
{
A<int> a;
a.f();
}

That is very interesting: I didn't know that one can define a
specialization of a member function without having to declare the
specialization version within class definition (declaration that is,
right?). I thought specialization followed overloading phenomenon.

Hmm?

Shouldn't the above instead be (will it work in the following form):

#include <iostream>

template <typename T>
class A {

private:
T t;
public:
void f(void){}
template<>
void f<int>(void)
};

template<>
void A<int>::f(void)
{
t++; // access private member
std::cout << "t tempered\n";
}

int main(void)
{
A<int> a;
a.f();
}

Sep 22 '05 #5
benben wrote:
And btw the following code shows how encapsulation is lost by providing a
member function specialization:

#include <iostream>

template <typename T>
class A
{
private: T t;
public: void f(void){}
};

template<>
void A<int>::f(void)
{
t++; // access private member
std::cout << "t tempered\n";
}

int main(void)
{
A<int> a;
a.f();
}


Isn't A<int>::f() accessing A<int>::t? And aren't the methods of a
class always allowed access to that class's private data members? Why
should we be surprised that this code compiles?

In fact, how would replacing the member template with a class method in
this example really make any difference at all? In short, I don't see
how this example illustrates anything unusual, surprising or at all out
of the ordinary.

Greg

Sep 22 '05 #6
* Greg:

#include <iostream>

template <typename T>
class A
{
private: T t;
public: void f(void){}
};

template<>
void A<int>::f(void)
{
t++; // access private member
std::cout << "t tempered\n";
}

int main(void)
{
A<int> a;
a.f();
}
...
how would replacing the member template with a class method in
this example really make any difference at all?


The difference is that with a member template, client code can very easily and
inadvertently access private things, whereas with an ordinary class member
function the client code wouldn't be able to supply an implementation (since
there would already be one), and it wouldn't be natural to try that, either.

In short, I don't see
how this example illustrates anything unusual, surprising or at all out
of the ordinary.


It doesn't. It illustrates a flaw in the C++ type system. There isn't any
lack of flaws in that type system, so it's not unusual, not surprising, not
out of the ordinary: it's just one more thing to keep in mind.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 22 '05 #7
> Shouldn't the above instead be (will it work in the following form):

#include <iostream>

template <typename T>
class A {

private:
T t;
public:
void f(void){}
template<>
void f<int>(void)


The above line seems to intend to provide a specialization of a non-template
member function in an instantiated class template, which, unsurprisingly,
cannot be understood by the compiler.

The code snip I posted showed a specialization of a non-template member
function in a class template, which is doable.

And the point is, anyone can legally abuse any class template by
specializing one or more of its members. Obviously, it will be fool to do
so.

Ben
Sep 22 '05 #8
"benben" <moc.liamtoh@hgnohneb read backward> wrote in message
news:43***********************@news.optusnet.com.a u

The code snip I posted showed a specialization of a non-template
member function in a class template, which is doable.

And the point is, anyone can legally abuse any class template by
specializing one or more of its members. Obviously, it will be fool
to do so.


I don't think it is either obvious or true. Since the choice of template
arguments for a template class is unlimited (both built-in and user defined
types), the original template class design it unlikely to be appropriate for
all possible template arguments.

The general template provides a default form of the concrete classes
instantiated from it, which hopefully is adequate most of the time, but
there will be plenty of occasions when a different specialisation will be
required. In many cases, the specialisation will be carried out by the
person who wrote the general template. Thus it may be something that occurs
*within* a library. I would agree that 3rd parties should exercise extra
caution when specialising members lest they inadvertently cause problems
with changes to private data.

--
John Carson
Sep 22 '05 #9
Hendrik Schober <Sp******@gmx.de> wrote:
[...]
I thought one cannot specialize only a member of a
class template, but has to specialize the whole
template. It seems that this isn't true?


Thanks to everyone who answered!

Hendrik Schober

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Coming back to where you started is not the same as never leaving"
Terry Pratchett
Sep 23 '05 #10

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

Similar topics

6
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
1
by: Imre | last post by:
Let's suppose we have a primary template with one argument defined in a header file. Two source files include this header, and both define a specialization of the primary template. Later, both...
1
by: Samee Zahur | last post by:
Why aren't we allowed to do partial specializations like these on numeric template parameters? That would have allowed us to do all kinds of interesting stuffs like loops of variable nesting (bad...
4
by: Alfonso Morra | last post by:
Does VC 7.1 support template specialization and partial specialization ?
2
by: Joseph Turian | last post by:
I'm posting this question for a friend who lacks USENET access. He and I were discussing this question and could not figure out the solution. Thank you for your help Joseph ===
4
by: stinos | last post by:
Hi All! suppose a class having a function for outputting data somehow, class X { template< class tType > void Output( const tType& arg ) { //default ToString handles integers/doubles
16
by: Hendrik Schober | last post by:
Hi, suppose we have template< typename T > struct X; and some specializations: template<>
1
by: jason.cipriani | last post by:
Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter),...
5
by: (2b|!2b)==? | last post by:
I would like to know if I can specialize only a specific method for a class template. Is the (specialization) code below valid? template <typename T1, typename T2> class MyClass { public:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
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
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.