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

"typedef templates" inside templates - how can I do this?

P: n/a
I'm aware, that up to date, "typedef templates" are not defined within
the C++ standard. The seemingly common workaround is this:
template <class T> struct MyTypeDef
{
/* ultimately I need something like this:
MyOtherClass<T> Type;
but let's keep it simple for the moment */

typedef short Type;
};

// I now can use this pseudo typedef template like this:

MyTypeDef<int>::Type foo;
// but this does not compile (MSVC):

template <class T> class MyClass {
MyTypeDef<T>::Type foo;
};
Is there a typo in my code, or is there some fundamental limitation that
prohibits the typedef template workaround in this situation?

-Sacha
Dec 6 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Sacha wrote:
I'm aware, that up to date, "typedef templates" are not defined within
the C++ standard. The seemingly common workaround is this:
template <class T> struct MyTypeDef
{
/* ultimately I need something like this:
MyOtherClass<T> Type;
but let's keep it simple for the moment */

typedef short Type;
};

// I now can use this pseudo typedef template like this:

MyTypeDef<int>::Type foo;
// but this does not compile (MSVC):

template <class T> class MyClass {
MyTypeDef<T>::Type foo;
typename MyTypeDef<T>::Type foo;
};


Gavin Deane

Dec 6 '05 #2

P: n/a
"Sacha" <sa**********@unibas.ch> wrote in message
news:43**************@unibas.ch
I'm aware, that up to date, "typedef templates" are not defined within
the C++ standard. The seemingly common workaround is this:
template <class T> struct MyTypeDef
{
/* ultimately I need something like this:
MyOtherClass<T> Type;
but let's keep it simple for the moment */

typedef short Type;
};

// I now can use this pseudo typedef template like this:

MyTypeDef<int>::Type foo;
// but this does not compile (MSVC):

template <class T> class MyClass {
MyTypeDef<T>::Type foo;
};


Make that

template <class T> class MyClass {
typename MyTypeDef<T>::Type foo;
};
--
John Carson
Dec 6 '05 #3

P: n/a

Sacha wrote:

template <class T> class MyClass {
MyTypeDef<T>::Type foo;
typename MyTypeDef<T>::Type foo;

This will work.
Is there a typo in my code, or is there some fundamental limitation that
prohibits the typedef template workaround in this situation?
Not a typo really. But typenames inside template qualified with the
template parameter must be accompanied by the keyword "typename", else
they will be treated as static variables.

-Sacha


Dec 6 '05 #4

P: n/a
Sacha wrote:
I'm aware, that up to date, "typedef templates" are not defined within
the C++ standard. The seemingly common workaround is this:
template <class T> struct MyTypeDef
{
/* ultimately I need something like this:
MyOtherClass<T> Type;
but let's keep it simple for the moment */

typedef short Type;
// Should that be
typedef T Type;
};

// I now can use this pseudo typedef template like this:

MyTypeDef<int>::Type foo;
// but this does not compile (MSVC):

template <class T> class MyClass {
MyTypeDef<T>::Type foo;
// Have to let the compiler know about the dependant type.
typename MyTypeDef<T>::Type foo;
};
Is there a typo in my code, or is there some fundamental limitation that
prohibits the typedef template workaround in this situation?


Does that sort you out?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Dec 6 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.