search

Home  >  Q&A  >  body text

c++ - 模板定义的基础问题

template <typename T>int compare(const T &v1, const T &v2)
{
    if (less<T>()(v1, v2)) return -1;//这里的less<T>后面为什么有两对括号?第一对为何是空的?
}
巴扎黑巴扎黑2885 days ago531

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 13:10:27

    less is a function object. The so-called function object is a class that overloads the call operator (that is, the "()" operator). Its object can be "used like a function", so it is called a function object. First A bracket indicates calling the constructor, and the following brackets are its parameters. This should be an example on C++ Primer. For details, please see the introduction to the function call operator on page 506 of C++ Primer 5th.

    reply
    0
  • Cancelreply