Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Parser error with freestanding template functions, class members and comparison with "<"

Steffen_M_
Beginner
375 Views
Hi,

we just stumbled over a weird parser error in ICC (both 13 and 15). The following code does not compile:

#include <iostream>

template<class T>
int bar ( const T& ) { return 0; }

struct Foo
{
  int bar;
};

template<class F> 
void callBar ( F& foo )
{
  if( foo.bar<0 )
    std::cout<<foo.bar<<std::endl;
}

int main ()
{
  Foo foo;
  foo.bar = -1;
  callBar( foo );
  return 0;
}

On both compilers, it fails with

test.cc(14): error: expected a ">"
    if( foo.bar < 0 )
                    ^

compilation aborted for test.cc (code 2)

The problem seems to be that for some reason, the "<" after "bar" causes the compiler to assume that "bar" refers to the freestanding template function. Accordingly, it interprets 0 as the template argument and then expects a ">" to end the template argument list. This looks a little bit like the inverse of the well-known situation where you have to precede template member function invocation by the "template" keyword as in "foo. template baz<2>()".

GCC also fails to compile the example above, while clang has no problems with it. Both ICC and GCC will happily accept the code after adding parentheses around "foo.bar".

Is this a defect, or is the compiler supposed to reject code like that according to the standard?

Thanks,
Steffen

0 Kudos
1 Reply
Om_S_Intel
Employee
375 Views

This looks to me a compiler error. I have submitted a report to Intel compiler development team on this. I will update the thread when I have more information.

 

Thanks,

 

Om

0 Kudos
Reply