By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Outside access to ACTUAL template parameter

P: n/a

The following won't compile for me with any of my compilers:

template<class SpecialType>
class MyClass {};
int main()
{
MyClass<int>::SpecialType obj;
}
The following compiles with Microsoft Visual C++, but I get a build error
with g++. According to the Standard, should it compile?
template<class SpecialType>
class MyClass {
public:

typedef SpecialType SpecialType;

};
int main()
{
MyClass<int>::SpecialType obj;
}
If the Standard forbids this, 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 until I know whether the Standard forbids my
second code snippet above.

(I'm writing reusable code, and at some point in the future, I may change
the internals so that "SpecialType" no longer coincides with the template
parameter, which is why I have the client write:

MyClass<int>::SpecialType obj;

rather than simply:

int obj;

--

Frederick Gotham
Jul 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Frederick Gotham" <fg*******@SPAM.comwrote in message
news:RZ*******************@news.indigo.ie...
>
The following won't compile for me with any of my compilers:

template<class SpecialType>
class MyClass {};
int main()
{
MyClass<int>::SpecialType obj;
}
The following compiles with Microsoft Visual C++, but I get a build error
with g++. According to the Standard, should it compile?
template<class SpecialType>
class MyClass {
public:

typedef SpecialType SpecialType;

};
I don't know what if anything the standard says about this but it seems to
me like a bad idea anyway. The meaning of this typedef looks like a Zen
puzzle to me. I think you can see why compilers might have a problem with
it, and it's completely unnecessary.
>

int main()
{
MyClass<int>::SpecialType obj;
}
If the Standard forbids this, 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 until I know whether the Standard forbids my
second code snippet above.
I usually typedef my template parameters in the class pretty much as you
show above. It never hurts and sometimes saves my bacon when I'm writing
other template classes and functions.

Look in the standard library implementations of classes. They do this a LOT.
>
(I'm writing reusable code, and at some point in the future, I may change
the internals so that "SpecialType" no longer coincides with the template
parameter, which is why I have the client write:

MyClass<int>::SpecialType obj;

rather than simply:

int obj;

--

Frederick Gotham

Jul 2 '06 #2

P: n/a
"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/
Jul 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.