Sunday, 15 May 2011

c++ - why primary expression don't include template_id -



c++ - why primary expression don't include template_id -

i have such code, compile "g++ -wall -g -std=c++11 test.cpp ",which doesn't compile because error: expected primary-expression before ‘)’ token

#include <functional> #include <vector> #include <algorithm> int main() { std::vector<int> vec; for(int i=0;i<10;++i) { vec.push_back(i); } std::sort(vec.begin(),vec.end(),std::less<int>); //should std::less<int>() }

but standard says:

primary-expression: literal ( look ) id-expression lambda-expression id-expression: unqualified-id qualified-id unqualified-id: identifier operator-function-id conversion-function-id literal-operator-id ~ class-name ~ decltype-specifier template-id template-id simple-template-id simple-template-id template-name <template-argument-list>

so seems std::less template-id, it's primary-expression.

being grammatically right doesn't create programme semantically correct. grammar production primary-expression -> template-id there allow utilize of specialization of function template expression, e.g, valid:

template <typename t> bool less(const t& a, const t& b) { homecoming < b; } int main() { std::vector<int> vec; std::sort(vec.begin(), vec.end(), less<int>); }

a template-id refers class template, however, not usable expression.

c++ bnf

No comments:

Post a Comment