0

The following c++ code compiles and works with Visual Studio 2017, but gives error "expected primary-expression before '>' token" with gcc 5.0. Any idea why? Description is a struct, and Description::add is a function template.

template <class X>
struct DataPoint
{
    X value;

    DataPoint()
    : value(0.) {}

    DataPoint( X value )
    : value(value) {}

    static void describe(Description< DataPoint<X> > & desc)
    {
        desc.add<X>("f", ".", offsetof(DataPoint<X>, value));
    }
}

In fact VS doesn't even complain when the DataPoint template type is not specified:

static void describe(Description<DataPoint> & desc)
{
    desc.add<X>("f", ".", offsetof(DataPoint, value));
}
psilouette
  • 83
  • 1
  • 6

1 Answers1

2

Your code is not standard C++. The add in desc.add<X> is a dependent name, so the compiler doesn't know if it is a template. Visual C++ still doesn't implement the entire two-phase lookup, so it's not surprising that it lets this through. But g++ (and clang, etc) require you to use the template keyword.

desc.template add<X>("f", ".", offsetof(DataPoint, value));
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720