Question

The following piece of C++ code:

template <typename T>
static void execute(T& obj) {
    obj.template operator()<int>();
}

when compiled with IBM's xlC 11.1 results in the error message: 1540-0063 (S) The text "operator" is unexpected. If I use a function instead of an operator, it works fine, but I do not want to rewrite a lot of library code. Is there any other way how to make the .template operator construct work with xlC?

Était-ce utile?

La solution

The best way to get the .template operator construct to work would be to submit a bug report to IBM.

This code conforms to the C++ standard. It is, in fact, the only legal way to write this code.


When you file the report you can use the following as evidence:

§14.2/4 states:

When the name of a member template specialization appears after . or -> in a postfix-expression ... and the object expression of the postfix-expression is type-dependent ... the member template name must be prefixed by the keyword template. ...


The first part: "the name of a member template specialization", applies because because operator()<int> is the name of a member template specialization. There is evidence for this in §14.2/1:

A template specialization can be referred to by a template-id:

§14.2/1 then specifies a syntax for a template-id which matches operator()<int>.


The second part: "and the object expression of the _postfix-expression_ is type-dependent", applies because obj is type-dependent. obj is type-dependent because of §14.6.2.2/3:

An id-expression is type-dependent if it contains: - an identifier associated by name lookup with one or more declarations declared with a dependent type, ...

In this case, obj is associated with T&, which is a dependent type (because T is a template parameter).


This is only half of a definitive answer, as I have not provided references for many claims that I made. I have been at this for an hour now though, so it is probably time to stop.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top