0

There's a template function f that requires its template parameter type T to have an inner class named Inner.

Inside f the class T::Inner shall be instantiated.

First try.

//
// "error: need 'typename' before 'T:: Inner' because 'T' is a dependent scope"   
// 
template <typename T>
void f( void )
{        
    T::Inner i;
}

I get that, so here comes the second try, where I don't get what's wrong:

/// "error: expected ';' before 'i'
template<typename T> 
void f ( void )
{                        
    typename T::Inner I;
    I i;
}

Why is that?

In my understanding: Inner is declared as type. The template has not yet been instantiated. Whether the type Inner exists or not first becomes relevant on instantiation - not definition. Where am I going wrong?

elrat
  • 70
  • 5

1 Answers1

2

I think you want to do

typename T::Inner i;

or

typedef typename T::Inner I;
I i;

whereas what you have in the question actually declares I to be a variable, and then right after that you are trying to use it as though it's a type.

Brian Bi
  • 91,815
  • 8
  • 136
  • 249
  • Works! Thank you very much. – elrat Jan 25 '17 at 00:20
  • I'd use `using I = typename T::Inner;` instead. And btw, IMO typename shouldn't be required on the RHS of an `using` statement, where it's clear that the only thing you can have is a type (or, if part of a `decltype`, it's also clear for the compiler what the RHS is). – vsoftco Jan 25 '17 at 00:28
  • 1
    @vsoftco write a paper and send it to std-proposals@isocpp.org – Brian Bi Jan 25 '17 at 00:34
  • @Brian Hahaha, let me first write a question here. Unfortunately I have other papers to write in my area :) But if I find that it may be a good idea, I'll never say never :) – vsoftco Jan 25 '17 at 00:35
  • @Brian Do you know where can I find some examples of such proposals? Are those the same as the Nxxxx papers, e.g. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4128.html? – vsoftco Jan 25 '17 at 01:08
  • @vsoftco Here is an example: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0222r0.html – Brian Bi Jan 25 '17 at 01:27
  • @Brian Thanks, this is very helpful! – vsoftco Jan 25 '17 at 01:56