[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Template specialization doesn't work after putting inside a namespace

tharinda.gl

12/17/2008 6:29:00 AM

Hi all,
I had a class with template methods and I have used
template specialization to handle special cases. But now the compiler
gives me errors after putting that class inside a namespace. What
should be done to get rid of this error?

This works

class A
{
template<typename T> MyFunc(T){}
template<> MyFunc<A*> MyFunc(A*){}
};

But this doesn't

namespace B
{
class A
{
template<typename T> MyFunc(T){}
template<> MyFunc<A*> MyFunc(A*){}
};
};


Thanks in Advance!

Tharinda
18 Answers

tharinda.gl

12/17/2008 6:30:00 AM

0

On Dec 17, 11:28 am, tharinda...@gmail.com wrote:
> Hi all,
>            I had a class with template methods and I have used
> template specialization to handle special cases. But now the compiler
> gives me errors after putting that class inside a namespace. What
> should be done to get rid of this error?
>
> This works
>
> class A
> {
>        template<typename T> MyFunc(T){}
>        template<> MyFunc<A*> MyFunc(A*){}
>
> };
>
> But this doesn't
>
> namespace B
> {
>      class A
>      {
>             template<typename T> MyFunc(T){}
>             template<> MyFunc<A*> MyFunc(A*){}
>      };
>
> };
>
> Thanks in Advance!
>
> Tharinda

I am using gcc version 3.4.3

Andrey Tarasevich

12/17/2008 6:51:00 AM

0

tharinda.gl@gmail.com wrote:
> This works
>
> class A
> {
> template<typename T> MyFunc(T){}
> template<> MyFunc<A*> MyFunc(A*){}
> };
>
> But this doesn't
>
> namespace B
> {
> class A
> {
> template<typename T> MyFunc(T){}
> template<> MyFunc<A*> MyFunc(A*){}
> };
> };

Neither works. In both cases the first declaration doesn't specify
return type for a function, which is illegal in C++. Both variants are
ill-formed. Post real code.

--
Best regards,
Andrey Tarasevich

Triple-DES

12/17/2008 7:21:00 AM

0

On 17 Des, 07:28, tharinda...@gmail.com wrote:
> Hi all,
>            I had a class with template methods and I have used
> template specialization to handle special cases. But now the compiler
> gives me errors after putting that class inside a namespace. What
> should be done to get rid of this error?
>
> This works
>
> class A
> {
>        template<typename T> MyFunc(T){}
>        template<> MyFunc<A*> MyFunc(A*){}
>
> };
>
> But this doesn't
>
> namespace B
> {
>      class A
>      {
>             template<typename T> MyFunc(T){}
>             template<> MyFunc<A*> MyFunc(A*){}
>      };
>
> };
>

There are quite a few trivial errors in the code you have posted.
Possibly you intented to write something like this:

class A
{
template<typename T> void MyFunc(T) {} // OK
};
template<> void A::MyFunc<A*>(A*) {} // OK

namespace B
{
class A
{
template<typename T> void MyFunc(T) {} // OK
};
template<> void A::MyFunc<A*>(A*) {} //OK
}

As you can see, the namespace should make no difference, but you have
to place your specializations outside the class, OR simply use
overloading instead, like this:
class A
{
template<typename T> void MyFunc(T) {} // OK
void MyFunc(A*) {} // OK
};

tharinda.gl

12/17/2008 7:34:00 AM

0

On Dec 17, 11:50 am, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> tharinda...@gmail.com wrote:
> > This works
>
> > class A
> > {
> >        template<typename T> MyFunc(T){}
> >        template<> MyFunc<A*> MyFunc(A*){}
> > };
>
> > But this doesn't
>
> > namespace B
> > {
> >      class A
> >      {
> >             template<typename T> MyFunc(T){}
> >             template<> MyFunc<A*> MyFunc(A*){}
> >      };
> > };
>
> Neither works. In both cases the first declaration doesn't specify
> return type for a function, which is illegal in C++. Both variants are
> ill-formed. Post real code.
>
> --
> Best regards,
> Andrey Tarasevich

Sorry Real Code was too big to be posted here. I am putting a
compilable code below

// This works

#include <iostream>

class A
{
public:
template<typename T> void PrintValue(T val);
};

template<typename T> void A::PrintValue(T val)
{
std::cout << "Primary Method " << val << std::endl;
}

template<> void A::PrintValue<double>(double dVal)
{
std::cout << "Specialized Method " << dVal << std::endl;
}

int main(int argc, char* argv[])
{
A objA;

objA.PrintValue<int>(5);
objA.PrintValue<double>(10.50);

return 0;
}

//This doesn't

#include <iostream>

namespace NS
{
class A
{
public:
template<typename T> void PrintValue(T val);
};
};

template<typename T> void NS::A::PrintValue(T val)
{
std::cout << "Primary Method " << val << std::endl;
}

template<> void NS::A::PrintValue<double>(double dVal)
{
std::cout << "Specialized Method " << dVal << std::endl;
}

int main(int argc, char* argv[])
{
NS::A objA;

objA.PrintValue<int>(5);
objA.PrintValue<double>(10.50);

return 0;
}

//These are the errors given by the compiler (gcc 3.4.3)

g++ Test.cpp
Test.cpp:19: error: specialization of `template<class T> void
NS::A::PrintValue(T)' in different namespace
Test.cpp:9: error: from definition of `template<class T> void
NS::A::PrintValue(T)'
Test.cpp:20: confused by earlier errors, bailing out

Thanks!

Tharinda

Andrey Tarasevich

12/17/2008 7:55:00 AM

0

tharinda.gl@gmail.com wrote:
>
> //This doesn't
>
> #include <iostream>
>
> namespace NS
> {
> class A
> {
> public:
> template<typename T> void PrintValue(T val);
> };
> };

}

Namespace definitions don't end with ';'.

> template<typename T> void NS::A::PrintValue(T val)
> {
> std::cout << "Primary Method " << val << std::endl;
> }
>
> template<> void NS::A::PrintValue<double>(double dVal)
> {
> std::cout << "Specialized Method " << dVal << std::endl;
> }
>
> int main(int argc, char* argv[])
> {
> NS::A objA;
>
> objA.PrintValue<int>(5);
> objA.PrintValue<double>(10.50);
>
> return 0;
> }
>
> //These are the errors given by the compiler (gcc 3.4.3)
>
> g++ Test.cpp
> Test.cpp:19: error: specialization of `template<class T> void
> NS::A::PrintValue(T)' in different namespace
> Test.cpp:9: error: from definition of `template<class T> void
> NS::A::PrintValue(T)'
> Test.cpp:20: confused by earlier errors, bailing out
>

The specialization for a member function template must be _declared_ in
the same namespace as the class itself (see 14.7.3/2). This means that
if you want to move the _definition_ of the member function template
somewhere else, you have to at least leave a _declaration_ inside the
namespace. In this code you violate this requirement: you are not
declaring the specialization of 'A::PrintValue<double>' inside the NS
namespace first.

In order to make the code compilable, add the declaration for the
specialization to the namespace

namespace NS
{
class A
{
public:
template<typename T> void PrintValue(T val);
};

template<> void A::PrintValue<double>(double dval); // <- added
}

template<typename T> void NS::A::PrintValue(T val)
{
// ...
}

template<> void NS::A::PrintValue<double>(double dVal)
{
// ...
}

In your first example (without namespace) class 'A' was a member of
global namespace. You _defined_ the specialization of
'A::PrintValue<double>' in global namespace as well. In that case the
definition served as declaration at the same time, so the above
requirement was naturally met.

So, alternatively, you can follow the same structure to make the code
with namespace NS compilable. In order to do that you have to move the
function definitions into the namespace

namespace NS
{
class A
{
public:
template<typename T> void PrintValue(T val);
};

template<typename T> void A::PrintValue(T val)
{
// ...
}

template<> void A::PrintValue<double>(double dVal)
{
// ...
}
}

This will also compile.

--
Best regards,
Andrey Tarasevich

tharinda.gl

12/17/2008 8:17:00 AM

0

On Dec 17, 12:55 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> tharinda...@gmail.com wrote:
>
> > //This doesn't
>
> > #include <iostream>
>
> > namespace NS
> > {
> >    class A
> >    {
> >    public:
> >            template<typename T> void PrintValue(T val);
> >    };
> > };
>
>    }
>
> Namespace definitions don't end with ';'.
>
>
>
> > template<typename T> void NS::A::PrintValue(T val)
> > {
> >    std::cout << "Primary Method " << val << std::endl;
> > }
>
> > template<> void NS::A::PrintValue<double>(double dVal)
> > {
> >    std::cout << "Specialized Method " << dVal << std::endl;
> > }
>
> > int main(int argc, char* argv[])
> > {
> >    NS::A objA;
>
> >    objA.PrintValue<int>(5);
> >    objA.PrintValue<double>(10.50);
>
> >    return 0;
> > }
>
> > //These are the errors given by the compiler (gcc 3.4.3)
>
> > g++ Test.cpp
> > Test.cpp:19: error: specialization of `template<class T> void
> > NS::A::PrintValue(T)' in different namespace
> > Test.cpp:9: error:   from definition of `template<class T> void
> > NS::A::PrintValue(T)'
> > Test.cpp:20: confused by earlier errors, bailing out
>
> The specialization for a member function template must be _declared_ in
> the same namespace as the class itself (see 14.7.3/2). This means that
> if you want to move the _definition_ of the member function template
> somewhere else, you have to at least leave a _declaration_ inside the
> namespace. In this code you violate this requirement: you are not
> declaring the specialization of 'A::PrintValue<double>' inside the NS
> namespace first.
>
> In order to make the code compilable, add the declaration for the
> specialization to the namespace
>
>    namespace NS
>    {
>      class A
>      {
>      public:
>        template<typename T> void PrintValue(T val);
>      };
>
>      template<> void A::PrintValue<double>(double dval); // <- added
>    }
>
>    template<typename T> void NS::A::PrintValue(T val)
>    {
>      // ...
>    }
>
>    template<> void NS::A::PrintValue<double>(double dVal)
>    {
>      // ...
>    }
>
> In your first example (without namespace) class 'A' was a member of
> global namespace. You _defined_ the specialization of
> 'A::PrintValue<double>' in global namespace as well. In that case the
> definition served as declaration at the same time, so the above
> requirement was naturally met.
>
> So, alternatively, you can follow the same structure to make the code
> with namespace NS compilable. In order to do that you have to move the
> function definitions into the namespace
>
>    namespace NS
>    {
>      class A
>      {
>      public:
>        template<typename T> void PrintValue(T val);
>      };
>
>      template<typename T> void A::PrintValue(T val)
>      {
>        // ...
>      }
>
>      template<> void A::PrintValue<double>(double dVal)
>      {
>        // ...
>      }
>    }
>
> This will also compile.
>
> --
> Best regards,
> Andrey Tarasevich

Thank you very much Andrey, you have clarified all my doubts

Regards,
Tharinda

Mocker

10/18/2012 3:52:00 PM

0

In article
<67afc76c-0343-4e25-bb40-109b7b99ba0d@s12g2000vbw.googlegroups.com>,
Nonplussed <lilhornie@yahoo.com> wrote:

> http://img.wpdigital.net/rf/image_606w/2010-2019/WashingtonPost/2012...
> torial-Opinion/Graphics/toles10182012.jpg
>
> Women and dogs.
>
> Mitt sees no difference.

so you don't want to become a Mormon?
Before you join up read a few of the things you will
have to believe if you want to become a Mormon.

I promise I am not making any of this up.

If you became a Mormon...
? you will be convinced that horses (mentioned in the Book of Mormon
as having lived in the Americas) are actually deer.1

? you will believe that Native Americans are Israeli immigrants.2

? you will believe that drinking coffee will get you a one way ticket
to hell.3

? you will deny the Mountain Meadows Massacre ever happened and if it
did it was committed by a few guys who never deserved to be brought to
justice.4

? you will believe that your blessed underwear will make you
fireproof.5

? you might subscribe to the belief that Dinosaur bones are from
distant galaxies that were brought together when Jesus made the earth
6,000 years ago.6

? you will believe that aliens from other planets are governed by
other gods. And that you too can be a god and have your own universe.7

? you will freely give 10% of your income to help fund a church that
has $30 Billion in assets.8

? you will believe that people who are gay are just individuals being
tempted by the devil to commit sin.9

? you will believe that the Earth is under populated and that women
need to have as many babies as possible.10

? you will secretly believe that polygamy should be practiced if only
the laws could be changed.11

? you will secretly worship a god-mother who birthed all good and bad
spirits.12

? you will believe that Cain still walks the Earth resembling the
Sasquatch and and is the father of all Africans ?a dark and cursed
people?13

? you will believe that if you are black you can literally become
white if you are righteous enough. You can also turn dark by being
bad.14

? you will believe that Native Americans had swords made of copper
long before steel was introduced by the Europeans.15

? you will believe that copper is strong enough to be made into a
sword.16

? you will believe in communalism and long for the day when all
Mormons will be gathered together to build a new city in Jackson
County, Missouri called the New Jerusalem.17
--
Karma ; what a concept!

Eddie Haskell

10/18/2012 5:05:00 PM

0


"man behind the curtain" <georgewk@toast.net> wrote in message
news:georgewk-F36842.08515118102012@news.toast.net...
> In article
> <67afc76c-0343-4e25-bb40-109b7b99ba0d@s12g2000vbw.googlegroups.com>,
> Nonplussed <lilhornie@yahoo.com> wrote:
>
>> http://img.wpdigital.net/rf/image_606w/2010-2019/WashingtonPost/2012...
>> torial-Opinion/Graphics/toles10182012.jpg
>>
>> Women and dogs.
>>
>> Mitt sees no difference.
>
> so you don't want to become a Mormon?
> Before you join up read a few of the things you will
> have to believe if you want to become a Mormon.
>
> I promise I am not making any of this up.

Keep posting. We'll let you know when anybody gives a damn.

-Eddie Haskell


Sanders Kaufman [MCSD]

10/18/2012 5:50:00 PM

0

"man behind the curtain" wrote in message
news:georgewk-F36842.08515118102012@news.toast.net...

>??? you will believe that Native Americans had swords made of copper
>long before steel was introduced by the Europeans.15

>??? you will believe that copper is strong enough to be made into a
>sword.16

Hahahahaha

Wayne

10/19/2012 4:51:00 PM

0



"Nonplussed" wrote in message
news:67afc76c-0343-4e25-bb40-109b7b99ba0d@s12g2000vbw.googlegroups.com...

> http://img.wpdigital.net/rf/image_606w/2010-2019/WashingtonPost/2012/10/17/Editorial-Opinion/Graphics/toles10...

> Women and dogs.

> Mitt sees no difference.

The "war on women" is complete bullshit. There is no country wide
discrimination against women.

What is difficult to express without getting liberal's panties all in a knot
is that frequently (but NOT always), women are not as valuable in the
workplace.

In the hourly wage world, a worker is hired at some hourly rate and paid on
the hours worked, not the work quality. And the men/women equality rates
are meticulously monitored by HR departments.

But, what about the world of salaried jobs (exempt from overtime, if you
prefer). The women I'm referring to are those who depart promptly from work
at 5PM, regardless of the workload, so that they can get home and have
dinner on the table for the kids at 6PM. Of course, their own husbands may
eat a cold dinner after he gets home from his job at 8PM.

Those are the positions where women unjustly whine about not getting equal
pay for equal work. They are NOT performing equal work.