R
Ralf Goertz
Hi,
I'm not sure whether this only applies to template classes but that's
where I stumbled over it.
Usually I try to inizialize variables not by assignment but by ctor,
i.e. "int i(0)" instead of "int i=0". But this doesn't work in the
following code:
#include <functional>
using namespace std;
template<class T> struct Odd : public unary_function<T,bool> {
bool operator() (T i) const {
return i%2;
}
};
template<class T> class X {
static Odd<T> odd;
};
template<class T> Odd<T> X<T>:
dd(Odd<T>()); // (*)
int main() {
X<int> x;
Odd<long> odder(Odd<long>()); // (**)
return 0;
}
Comeau gives:
"ComeauTest.c", line 15: error: declaration is incompatible with "Odd<T>
X<T>:
dd"
(declared at line 12)
template<class T> Odd<T> X<T>:
dd(Odd<T>());
^
g++ gives:
x.cc:15: error: no ‘Odd<T> X<T>:
dd(Odd<T> (*)())’ member function declared in
class ‘X<T>’
If I replace (*) with
template<class T> Odd<T> X<T>:
dd=Odd<T>();
it works, which is even odder since the ctor construction works in the
line marked by (**). By the way, why is it necessary to have a static
member of a template initialized at all?
I'm not sure whether this only applies to template classes but that's
where I stumbled over it.
Usually I try to inizialize variables not by assignment but by ctor,
i.e. "int i(0)" instead of "int i=0". But this doesn't work in the
following code:
#include <functional>
using namespace std;
template<class T> struct Odd : public unary_function<T,bool> {
bool operator() (T i) const {
return i%2;
}
};
template<class T> class X {
static Odd<T> odd;
};
template<class T> Odd<T> X<T>:
int main() {
X<int> x;
Odd<long> odder(Odd<long>()); // (**)
return 0;
}
Comeau gives:
"ComeauTest.c", line 15: error: declaration is incompatible with "Odd<T>
X<T>:
(declared at line 12)
template<class T> Odd<T> X<T>:
^
g++ gives:
x.cc:15: error: no ‘Odd<T> X<T>:
class ‘X<T>’
If I replace (*) with
template<class T> Odd<T> X<T>:
it works, which is even odder since the ctor construction works in the
line marked by (**). By the way, why is it necessary to have a static
member of a template initialized at all?