"Frederick Gotham" <fg*******@SPAM.comwrote:
The following won't compile for me with any of my compilers:
template<class SpecialType>
class MyClass {};
int main()
{
MyClass<int>::SpecialType obj;
}
The class "MyClass<int>" has no member neamed "SpecialType",
so I wouldn't expect this to compile.
The following compiles with Microsoft Visual C++, but I get a
build error with g++.
According to the Standard, should it compile?
I don't see anything in section 14 (Templates) that requires
the code you give to compile, no.
template<class SpecialType>
class MyClass {
public:
typedef SpecialType SpecialType;
};
int main()
{
MyClass<int>::SpecialType obj;
}
If the Standard forbids this,
Say, rather, the std. doesn't seem to require compilers to recognize
your syntax.
then the less favourable work-around would be:
template<class SpecialType_T>
class MyClass {
public:
typedef SpecialType_T SpecialType;
};
int main()
{
MyClass<int>::SpecialType obj;
}
But I don't want to do that...
Why not? If you look at any STL reference book, you'll see that
many template classes in STL have "value_type" defined. That seems
to be the way to do it. So why don't you just do this:
#include <iostream>
template<typename T>
class MyClass
{
public:
typedef T value_type;
MyClass(T a) : a_(a) {}
T get_a() {return a_;}
private:
T a_;
};
int main()
{
MyClass<int>::value_type Object = 7;
MyClass<intWidget (Object);
std::cout << Widget.get_a() << std::endl;
return 0;
}
(A fancy way to display "7".)
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/