last day (14 days later) » 

5:04 AM
Hi nshct. Thanks for taking time to explain this further.
 
No problem.
First, terminology: A "symbol" is simply a construct that is labeled by an "identifier"
For example, class member functions are symbols
 
Gotcha.
 
At first, while evaluating a symbol, a compiler tries to make sense of what "is behind" that symbol.
Sometimes, a symbol is a local variable; sometimes it is a name of Type
In a strongly typed language like C++ it is really important that the compiler knows at any time what symbols mean. That's the whole point.
 
Ok. With you so far.
 
If we now look at a template class, let's name it my_class<T>, we can see that there are in fact infinite amount of classes there
We can substitute the T with any type we like and we receive a type
 
5:08 AM
Right.
 
But depending on T, these classes might contain different symbols.
One can use template specialization for example to do different things for different types.
But how can a compiler then know what my_class<T>::my_name means if it does not know T?
But it has to know, because it is statically typed! So we have a problem here.
Imagine this function:
template< typename T > void test( )
{ using U = my_class<T>::my_name }
As we already discovered, for different Ts, my_class might contain different symbols.
So we can't be sure if my_name is a type. But it is required that my_name is a type because we are trying to create an alias here.
Are you following me?
 
Sorry, what do you mean by "we can't be sure if my_name is a type"?
 
Well, we said that my_class<T> might contain different symbols depending on T. Let's say that for T = int we include a static member: static int my_name;. But for any other T that is not int we include a type: using my_name = void
So, for T = int, my_class<T>::my_type is a static member, but for any other T it is a type alias
That's the problem. It can be anything, we can't say
We dont know yet, because we do not know T yet
 
I see what you mean. But I wasn't aware this was possible. I get that if T = int, we include static int my_name. But if T were double, we would include static double my_name, no?
I.e. regardless of what T is, we include a static member variable of type "whatever is T"...?
 
No, you can decide for particular T's to do something entirely differnt
 
5:16 AM
Huh. I never knew that.
 
Yea, it is called template specialization. en.cppreference.com/w/cpp/language/template_specialization
So yea, you see: It can happen!
So we can never tell what the type is.
So the compiler doesn't do anything.
 
OOOOOH. I'm with you now. I get it. It's because of template specialization. Ok, I'm with you now.
 
We have to explicitely tell him that we know that for every T it will be a type.
If we lie, that's an error.
using U = typename my_class<T>::my_name
that would be a lie
because for T = int, it is not a typename
In your case, map<T, ...>::iterator is always a type.
We know that, but the compiler doesn't
because there is this "symbol" Inner which we don't know if thats a type or not
Because, as in the other example, Owner<C, T>::Inner could be a static int for T = void
Compiler has to expect that
So we have to put the keyword here to guarantuee the compiler that the symbol is a typename:
typename std::map<C, Inner>::iterator
 
I gotta chew on this a bit, but I'm circling around understanding.
 
You have to remember that once you use a dependent name in an expression, the whole expression becomes dependent
Sure. I hope I could help a little bit.
 
5:22 AM
This was above and beyond the level of help I could have expected. Thank you so much. I'm going to save the text of this chat and other examples you sent me.
 
Yea I have to delete this because I might have done a rule violation (?)
I think it is not allowed to do this kind of chat here?
I dont know.
 
I'm not really sure. I'm not completely savvy on all the rules of etiquette on StackOverflow.
If there's something I can do to bump up your StackOverflow reputation other than checking the accept answer checkmark, I'll certainly do so. Thank you again.
 
No. No need to break the rules.
 
Alright. Good night, and thanks again. Really appreciate you spending your time to answer this thoroughly.
 
No problem. I figured there isn't enough space to do a thorough explanation in the comments of an answer.
 

last day (14 days later) »