Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
33 Views

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

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
Highlighted
Employee
33 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