为什么我不能定义比较没有常数

时间:2012-05-27 20:15:36

标签: c++ compiler-construction g++

如果我像这样定义compare函数:

bool compare(Student& a, Student& b)
{
    return a.n < b.n;
}

g ++会抱怨:

g++ -Wall main.cpp -o main
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/algorithm:63:0,
                 from main.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Tp = Student, _Compare = bool (*)(Student&, Student&)]’:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2261:78:   instantiated from ‘_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Compare = bool (*)(Student&, Student&)]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2302:62:   instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Size = long int, _Compare = bool (*)(Student&, Student&)]’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:5250:4:   instantiated from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Student*, std::vector<Student> >, _Compare = bool (*)(Student&, Student&)]’
main.cpp:38:51:   instantiated from here
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2229:4: error: invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’
/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_algo.h:2232:4: error: invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’

Compilation exited abnormally with code 1 at Mon May 28 08:05:35

但是如果我用const类型定义比较,它将编译并正常工作。

以下是所有代码:

class Student {
public:
    Student(string);
    string n;

};

bool compare(Student& a, Student& b)
{
    return a.n < b.n;
}

Student::Student(string name) { n = name; }

int main()
{
    Student A = Student("A");
    Student B = Student("B");

    vector<Student> students;
    students.push_back(B);
    students.push_back(A);

    sort(students.begin(), students.end(), compare);

    cout << "After sort" << endl;
    for(vector<Student>::iterator i = students.begin(); i != students.end(); ++i) {
        cout << "Student: " << i->n << endl;
    }

    return 0;
}

3 个答案:

答案 0 :(得分:6)

在此实施中,std::sort使用

 const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare);

在您的情况下,_Tp是学生,_Comparecompare

所以你基本上有

const Student& std::__median(const Student&, const Student&, const Student&, 
                                                   bool (*)(Student&, Student&) )

或类似的。显然,回调不能应用于它们被转换为const的参数,所以失败。

将参数设为compare方法const

答案 1 :(得分:2)

我不相信标准中要求函数的参数必须是const,所以我认为你的实现在拒绝它时是错误的。但是,要求函数不修改参数:

从标准--25.4 / 2

  

Compare是一个函数对象类型(20.8)。的返回值   函数调用操作应用于Compare类型的对象,何时   上下文转换为bool(4),如果是第一个参数,则返回true   呼叫小于第二个,否则为假。比较comp   在整个过程中用于假设有序关系的算法。它是   假设comp不会通过的应用任何非常数函数   解除引用的迭代器。

来自25.4.1.1

std::sort签名
template<class RandomAccessIterator, class Compare>
void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp)

因为你的函数不允许修改它的参数,所以它真的应该把它们作为const,但是标准不要求它。因此,虽然您的实现可能是错误的,但我认为这是一个可以原谅的错误,因为它设法通过修改其参数或者它不是const-correct来引起注意事实,即您的函数违反了标准。 / p>

答案 2 :(得分:0)

你应该使它成为const,因为它使两个参数保持不变。

相关问题