By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

Template<> confusion

P: n/a
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

Feb 13 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a

"SoilMan" <cb****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.
--
Sumit Rajan <su****@msdc.hcltech.com>
Feb 13 '06 #2

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

Feb 13 '06 #3

P: n/a

SoilMan wrote:
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.

Feb 13 '06 #4

P: n/a
dc wrote:
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.

Feb 13 '06 #5

P: n/a
Sumit Rajan wrote:
"SoilMan" <cb****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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;
}


Just out of curiosity, shouldn't it be:

template<>
void xyz::foo<int>(int x)
{
cout << "foo<int> " << x << endl;
}
Feb 13 '06 #6

P: n/a

"red floyd" <no*****@here.dude> wrote in message
news:Rd******************@newssvr11.news.prodigy.c om...
Sumit Rajan wrote:

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;
}


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.
--
Sumit Rajan <su****@msdc.hcltech.com>
Feb 14 '06 #7

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

Feb 14 '06 #8

P: n/a

Rolf Magnus escreveu:
dc wrote:
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.


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.

Feb 14 '06 #9

P: n/a
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.

Feb 14 '06 #10

P: n/a
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

Feb 14 '06 #11

P: n/a
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;
}

Feb 15 '06 #12

P: n/a

dc escreveu:

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;
}


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

Feb 15 '06 #13

P: n/a
dc wrote:
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.

Feb 16 '06 #14

P: n/a
Rolf Magnus <ra******@t-online.de> wrote:
dc wrote:
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.


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

test(static_cast<short>(2));

--
Marcus Kwok
Feb 16 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.