Template<> confusion

S

SoilMan

Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

This code compiles on MS VS C++ 2003 and Intel C++ 9 but not on Dev-C++
4.9.9.2. However, if I comment out the line "template<>", it compiles
fine on all 3 compilers.

Can someone please explain what is the standard C++ on this matter?
Thank you.

Chris
 
S

Sumit Rajan

SoilMan said:
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

Move the following out of this scope:
template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

and place it here:

template <>
void xyz::foo(int x)
{
cout << "foo<int> " << x << endl;
}

Regards,
Sumit.
 
M

Maxim Yegorushkin

SoilMan said:
Consider the following:

class xyz
{
public:
template <typename T>
void foo(T x)
{
cout << "foo<T> " << x << endl;
}

template <> // remove this line or not?
void foo(int x)
{
cout << "foo<int> " << x << endl;
}
};

This code compiles on MS VS C++ 2003 and Intel C++ 9 but not on Dev-C++
4.9.9.2. However, if I comment out the line "template<>", it compiles
fine on all 3 compilers.

Can someone please explain what is the standard C++ on this matter?

A specialization of member template can only be declared out of the
class definition.

Anyway, you don't need specialization here, function overloading would
suffice.
 
R

Rolf Magnus

dc said:
There is no need to place template <> when u defining a specific
implementation.

It depends on what you want. If you don't place the template<>, you get an
overload instead of a specialization.
 
R

red floyd

Sumit said:
Move the following out of this scope:

and place it here:

template <>
void xyz::foo(int x)
{
cout << "foo<int> " << x << endl;
}

Just out of curiosity, shouldn't it be:

template<>
void xyz::foo<int>(int x)
{
cout << "foo<int> " << x << endl;
}
 
S

Sumit Rajan

red floyd said:
Sumit Rajan wrote:

Just out of curiosity, shouldn't it be:

template<>
void xyz::foo<int>(int x)
{
cout << "foo<int> " << x << endl;
}

IIRC, it is not necessary.

Regards,
Sumit.
 
D

dc

Can someone explain when u need overloaded version / specialization of
a template. And
how does it differs...
 
M

Marcelo Pinto

Rolf Magnus escreveu:
It depends on what you want. If you don't place the template<>, you get an
overload instead of a specialization.

IIRC, in either case you get an overload, for there is no such thing as
function template specialization.

Please advise me if I got it wrong.

TIA,

Marcelo Pinto.
 
D

dc

Ya , for function it doesnot matter. If both implemented , overload
will be preferred over
specialization.
But for class template, only specialization is the solution.
 
M

Marcelo Pinto

dc wrote:

Please, do cote what you are replying to.
Ya , for function it doesnot matter. If both implemented , overload
will be preferred over
specialization.

IIRC, there is *no* such thing as function template specialization,
just function template overloading. Please, advise me if I am wrong.
But for class template, only specialization is the solution.

Agreed, for there is no "overloaded class".

Regards,

Marcelo Pinto
 
D

dc

IIRC, there is *no* such thing as function template specialization,
just function template overloading. Please, advise me if I am wrong.

No body prevents u from implementing the function template
specialization.
template <class T>
void func(T i){
}

//Here is function template specialization
template <>
void func(int i){
cout << "func spec" <<endl;
}

void func(int i){
cout << "func overload" <<endl;
}
 
M

Marcelo Pinto

dc escreveu:
No body prevents u from implementing the function template
specialization.
template <class T>
void func(T i){
}

//Here is function template specialization
template <>
void func(int i){
cout << "func spec" <<endl;
}

void func(int i){
cout << "func overload" <<endl;
}

I am sory, I recalled it incorrectly. You can't partial specialize a
function template, but you *can* full specialize a function template.

Sory,

Marcelo Pinto
 
R

Rolf Magnus

dc said:
Can someone explain when u need overloaded version / specialization of
a template. And
how does it differs...

I'd prefer specialization to avoid confusion. Consider this:

#include<iostream>

template <typename T>
void test(T value)
{
std::cout << "not a short\n";
}

void test(short value)
{
std::cout << "a short with value " << value << '\n';
}

int main()
{
test<short>(2);
}

If the template argument cannot be deduced from the function arguments, you
can't even use the overload version.
 
M

Marcus Kwok

Rolf Magnus said:
I'd prefer specialization to avoid confusion. Consider this:

#include<iostream>

template <typename T>
void test(T value)
{
std::cout << "not a short\n";
}

void test(short value)
{
std::cout << "a short with value " << value << '\n';
}

int main()
{
test<short>(2);
}

If the template argument cannot be deduced from the function arguments, you
can't even use the overload version.

However, you can still call the overload version by explicitly
indicating the type of the argument by using

test(static_cast<short>(2));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top