Template function broken after compiling with C++11

  • A+
Category:Languages

I have a very simple template function to compare the rank field of two structs:

template<typename T> bool comp_rank(const T &a, const T &b){     return a.rank < b.rank; } 

This worked fine, until I compiled with -std=c++11. Now, I get the error

error: parameter "b" is not a type name       return a.rank < b.rank;                       ^  ./src/util.h(123): error: expected a ">"       return a.rank < b.rank;                        ^ 

What gives? This seems like basic syntax that I would surprised to find had changed after C++11.

 


Your problem is that you use

using namespace std; 

Because c++11 introduces a template rank

See here : https://en.cppreference.com/w/cpp/types/rank

If you remove the using statement, everything compiles fine again!

Here is the error message given by gcc 8.2.1

main.cpp: In function 'bool comp_rank(const T&, const T&)': main.cpp:41:23: error: type/value mismatch at argument 1 in template parameter list for 'template<class> struct std::rank'     return a.rank < b.rank; 

It is not a good idea to use using namespace ... anyway. You gave already a good example ;)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: