Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

keyword typename being used outside of a template definition

27 views
Skip to first unread message

Bob Langelaan

unread,
May 24, 2020, 11:00:55 AM5/24/20
to
I came across the following code:

typename Chain::Node * Chain::walk(Node * curr, int k){

if (k==0 || curr == NULL)
return curr;
else
return walk(curr->next,k-1);
}

Can anyone explain what the purpose is for the keyword typename in the code above?

Also, if it is a new feature, which I suspect, is it C++ 14, C++ 17 or C++ 20?

Thanks!

Sam

unread,
May 24, 2020, 11:32:05 AM5/24/20
to
Bob Langelaan writes:

> I came across the following code:
>
> typename Chain::Node * Chain::walk(Node * curr, int k){
>
> if (k==0 || curr == NULL)
> return curr;
> else
> return walk(curr->next,k-1);
> }
>
> Can anyone explain what the purpose is for the keyword typename in the code
> above?

typename is really just an alias for "class".

> Also, if it is a new feature, which I suspect, is it C++ 14, C++ 17 or C++
> 20?

You could always use typename as an alias for "class". And, like "class"
itself, in this context, it's mostly superfluous.

Alf P. Steinbach

unread,
May 24, 2020, 11:35:18 AM5/24/20
to
You can see from the recursive "let's get some UB!" C++ implementation
of a linear complexity algorithm, that it's written by an incompetent.

So that's essentially the answer.

If this is actually outside any template then I doubt that it's valid.


- Alf

Bob Langelaan

unread,
May 24, 2020, 12:13:41 PM5/24/20
to
This code compiles and is definitely not inside a template definition.

Some of the code written by the same individual seems reasonably competent such as:

void list_test( string msg, Node *actual,
initializer_list<int> expect ) {
vector<int> act = to_vector( actual );
vector<int> exp;
cout << msg << ": ";
print( actual );
for( auto elem : expect ) {
exp.push_back(elem);
}
if( act == exp ) return;
cerr << "*ERROR* " << vectorStr(act) << " != (expected) "
<< vectorStr(exp) << "\n";
}

Bob Langelaan

unread,
May 24, 2020, 12:18:32 PM5/24/20
to
I knew that in a template definition, typename can usually be replaced by class. So I guess I am asking what is the purpose of prefixing the return type of a function with the keyword typename (or class)?

Bo Persson

unread,
May 24, 2020, 12:30:35 PM5/24/20
to
There is no purpose, unless Chain is a nasty #define for a template.
However, the syntax probably allows for a redundant typename even when
it is not really needed.

In other cases 'typename' is requires even in places where nothing but a
type is possible. :-)

Some effort has been done to try to sort this out

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0634r1.html

but the work is far from complete.


Bo Persson


Alf P. Steinbach

unread,
May 24, 2020, 12:44:39 PM5/24/20
to
My eyes.

- Alf

Chris Vine

unread,
May 24, 2020, 3:07:42 PM5/24/20
to
'typename' could not necessarily be replaced by 'class' here. In this
usage the 'typename' keyword indicates that Node is a nested
(dependent) type defined in class Chain, which might for example be
defined in that class by a typedef or the using keyword. Employing
'class' in place of 'typename' would indicate that Node is a nested
class or struct type defined within a class Chain or a Chain namespace.

Typical usage of 'typename' here would be if 'walk' is a method of a
template class having Chain as a template parameter. It would
indicate that Chain::Node is a dependant type of Chain. If there is no
template definition involved here, then assuming Chain::Node is indeed
a type alias, in C++11 onwards 'typename' is harmless but redundant.
With C++98/03 I think it would be an error.

Chris Vine

unread,
May 24, 2020, 3:15:40 PM5/24/20
to
By the way, if that were the case here then I would expect compilation
to fail because the type of argument 'curr' is not qualified as a
dependent type of Chain.

Bob Langelaan

unread,
May 24, 2020, 11:34:39 PM5/24/20
to
I can confirm that Node a nested class defined inside of Chain.

I can also confirm that removing typename does not create any additional errors or warnings on the Community version of MS VS 2019. It is possible the code was written for another compiler which required the typename I suppose.

Bonita Montero

unread,
May 26, 2020, 10:44:39 AM5/26/20
to
> Can anyone explain what the purpose is for the keyword typename in the code above?

It's because C++-parsers are stupid.

0 new messages