Re: syntax error in std::vector<T>::const_iterator where T = (class) U<V>
On Feb 8, 6:23 am, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze <james.ka...@gmail.com> wrote:
In a lot of cases, context would allow the compiler to make it
clear: only a type is legal, or a type is not legal, but the
committee decided to not require the compiler to take any
context into consideration.
The following doesn't compile. Why shouldn't it? I see no rational
reason why the compiler should refuse to compile it.
Because the standard says that it shouldn't compile:-). You've
presented an interesting question, however...
//---------------------------------------------------------------
#include <iostream>
template<typename T>
void foo()
{
T::f(); // Is 'f' a function or a type?
What's interesting here is that the syntax is remarkably similar
in both cases. Formally, if f is a function, this line is
a function call; if it's a type, the line is an "explicit type
conversion (funtional notation)". But unlike a lot of the other
cases, it's easy to imagine an implementation which doesn't
really distinguish between the two until far later. The choice,
here, can't have any impact on later code.
Still, the standard says that when parsing the template, the
compiler is required to assume that f is not a type.
}
struct A
{
// 'f' is a function
static void f() { std::cout << "A\n"; }
};
struct B
{
// 'f' is a type
struct f
{
f() { std::cout << "B\n"; }
};
};
int main()
{
foo<A>();
foo<B>(); // Fails because B::f is a type}
//---------------------------------------------------------------
That's what the standard requires.
I don't see why the compiler couldn't defer the decision in this
case; I suppose that the only reason it doesn't is to be
orthogonal with cases where it must know.
--
James Kanze