tags:

views:

122

answers:

2
"A definition for a static data member may be provided in a namespace scope
 enclosing the definition of the static member's class template."

It means ...

Is this correct.....

namespace N{
template<typename T>class A{
public: 
   static int x;
   static int fun();
 };

}
namespace N1{
template<class T>int A<T>::x=10; }
namespace N2{template<class T>int A<T>::fun(){return 10;} }
int main(){  return 0; }

According to the statement...whether my program is correct...

Otherwise...ca any one expalain this statement...With a program.... IT is the point from ISO Standard c++.chapter 14.5.1.3, point 1

+4  A: 

If you check the standard you should see the example.

A definition for a static data member may be provided in a namespace scope enclosing the definition of the static member’s class template. [Example:

template<class T> class X {
    static T s;
};
template<class T> T X<T>::s = 0;

—end example]

Your program is not correct because

template<class T> A<int>::x=10;
template<class T> A<int>::fun(){return 10;}

did not use the template parameter T (and do you mean Ex instead of A?). Since you're specializing a template, you should write

template<> int Ex<int>::x = 10;
template<> int Ex<int>::fun() { return 10; }

within the namespace N. (Not inside other namespaces like N1 or N2. It must be defined in the same namespace where Ex is declared.)

KennyTM
@BE: Not reproducible. http://www.ideone.com/T2kZ6
KennyTM
+1  A: 

In your example, you do not specify the namespace of your static definitions, as you close it before the static statements.

In addition you do not give a return type for the definition of fun().

This should be better:

namespace N{
template<typename T>class Ex{
public: 
    static int x;
    static int fun();
}

}
template<class T> N::Ex<T>::x=static_cast<T>(0);
template<class T> T N::Ex<T>::fun(){return static_cast<T>(10);}
int main(){  return 0; }

Assuming your template parameter T is somehow numerical.

UPDATE:

Correcting a few things in you code, this one compiles with g++:

namespace N{
    template<class T> class Ex{
    public:
        static T x;
        static T fun();
    };
}
template<class T> T N::Ex<T>::x=static_cast<T>(0);
template<class T> T N::Ex<T>::fun(){return static_cast<T>(10);}
int main(){  return 0; }
Cedric H.
Maybe you can show us what error you get ...
Cedric H.
Corrected my answer and the example code.
Cedric H.