compiler error???

compiler error???

template
struct FIRST
{
template
struct SECOND
{
enum { val = 0 };
};

};

template
class THIRD
{
public:
enum
{
value = FIRST::SECOND::val
};

};

int main(int argc, char* argv[])
{
int a = THIRD::value;
return 0;
}

gives (intel compiler 6.0.1.305):

C:TEST est04 est04.cpp(26): error: type name is not allowed
value = FIRST::SECOND::val

(and the arrow points to T2)

VC++ 6.0sp5 instead accepts the code.

3 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

The confusion is caused by the fact that SECOND is not interpreted as a template name, so < is interpreted as a comparison operator, and the compiler tries to interpret T2 as an expression.

In this program, it's "obvious" that SECOND should be interpreted as a template in the context of an instance of FIRST. However, what if FIRST were specialized for some value of its template parameter, and the specialization didn't contain a member template named SECOND? It might instead contain a member constant named SECOND. That would radically change the parsing of the expression, and would likely result in massive confusion.

The solution is to insert the "template" keyword between "::" and "SECOND", to force the compiler to assume that SECOND (always) refers to a member template. That would make the code standard-conforming.

However, since Microsoft's compiler interprets this "correctly" without the "template" keyword, Intel's compiler should be able to do so also. This should probably be reported as a bug through Premier Support.

> The confusion is caused by the fact that SECOND is
> not interpreted as a template name, so < is
> interpreted as a comparison operator, and the
> compiler tries to interpret T2 as an expression.

thanks for your kind reply.
now I completely understand the problem.

The "template SECOND" solution was also pointed out in a VC++ specific newsgroup, where they pointed out that, according to the standard, VC is wrong and ICL is right.

> However, since Microsoft's compiler interprets this
> "correctly" without the "template" keyword, Intel's
> compiler should be able to do so also.

I think a more detailed error message would suffice: maybe the compiler could pick a different error code when parsing expressions with templates.

Connectez-vous pour laisser un commentaire.