Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

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

$
0
0
​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


Viewing all articles
Browse latest Browse all 1616

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>