Help needed - template type issue

J

JT

I have a compiler error when using a non-dependent type declared in a
template, if I use the type in function definitions. I think it is a
parsing issues related to confusion with a typename. Below I have a
simple example. Is there another solution I should be using instead of
declaring the type outside of the template?

Thanks,
JT


For a class, this works, and the typedef is available in the function
definition...

class Test
{
typedef int myIntTypedef;
myIntTypedef funct();
};

Test::myIntTypedef Test::funct()
{
return 0;
}

For a template, however, the above does not work, because (I think) the
type is not available. The compiler (VC++7.1) suggests I use typename
for the type, which is not correct in this case.

template <class T>
class Test2
{
typedef int myIntTypedef;
myIntTypedef funct();
};

template <class T>
Test2<T>::myIntTypedef Test2<T>::funct() // error "dependent name is
not a type"
{
return 0;
}

If I define the type outside of the template, it will work...

typedef int myIntTypedef;

template <class T>
class Test3
{
myIntTypedef funct();
};

template <class T>
Test3<T>::myIntTypedef Test3<T>::funct()
{
return 0;
}
 
V

Victor Bazarov

JT said:
I have a compiler error when using a non-dependent type declared in a
template, if I use the type in function definitions. I think it is a
parsing issues related to confusion with a typename. Below I have a
simple example. Is there another solution I should be using instead of
declaring the type outside of the template?

Thanks,
JT


For a class, this works, and the typedef is available in the function
definition...

class Test
{
typedef int myIntTypedef;
myIntTypedef funct();
};

Test::myIntTypedef Test::funct()
{
return 0;
}

For a template, however, the above does not work, because (I think) the
type is not available. The compiler (VC++7.1) suggests I use typename
for the type, which is not correct in this case.

How do _you_ know that it's not correct? If I define a specialisation
of your Test2 template like this:

template<> class Test2<char>
{
static std::string myIntTypedef;
};

then 'myIntTypedef' will NOT be a type-id for the Test2<char> but will
still be a type-id for any other Test2 instantiation. That means that
'myIntTypedef' _is_ type-dependent.
template <class T>
class Test2
{
typedef int myIntTypedef;
myIntTypedef funct();
};

template <class T>
Test2<T>::myIntTypedef Test2<T>::funct() // error "dependent name is
not a type"

This should be

template<class T>
 
J

John Harrison

JT said:
I have a compiler error when using a non-dependent type declared in a
template, if I use the type in function definitions. I think it is a
parsing issues related to confusion with a typename. Below I have a
simple example. Is there another solution I should be using instead of
declaring the type outside of the template?

Thanks,
JT


For a class, this works, and the typedef is available in the function
definition...

class Test
{
typedef int myIntTypedef;
myIntTypedef funct();
};

Test::myIntTypedef Test::funct()
{
return 0;
}

For a template, however, the above does not work, because (I think) the
type is not available. The compiler (VC++7.1) suggests I use typename
for the type, which is not correct in this case.

Not sure why you think that.
template <class T>
class Test2
{
typedef int myIntTypedef;
myIntTypedef funct();
};

template <class T>
Test2<T>::myIntTypedef Test2<T>::funct() // error "dependent name is
not a type"
{
return 0;
}

This compiles fine for me.

template <class T>
typename Test2<T>::myIntTypedef Test2<T>::funct()
{
return 0;
}

Compiler error message was correct as far as I can see.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top