tharinda.gl
12/17/2008 8:17:00 AM
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