2012-03-24 70 views
1

任何人都可以说明为什么这个函数对象不需要指定的类型吗?为什么这个函数对象不需要指定?

class StringPtrTmplLess 
{ 
public: 
    template<typename PtrType> 
    bool operator()(const PtrType * lhs, const PtrType * rhs) 
    { 
     return *lhs < *rhs; 
    } 
}; 

int main() 
{ 
    set<string*, StringPtrTmplLess> s2; 
    return 0; 
} 

编译器如何知道它将初始化StringPtrTmplLess的指定类型?

回答

3

这是因为template argument deduction这意味着模板参数是从传递给函数调用的参数的类型推导出。这种类型的推导是由编译器完成的。请通过the link详细解释。

相关问题