我在使用Sun Studio编译器的Solaris上遇到问题,这很可能是由于使用了奇怪的STL实现( libCstd ),请参阅http://developers.sun.com/solaris/articles/cmp_stlport_libCstd.html。考虑一下:
std::vector<C*> v;
// .. populate the vector
std::sort(v.begin(), v.end());
其中C
是某个类。这会产生以下编译器错误消息:
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm", line 725: Error: The operand "*first" cannot be assigned to.
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm.cc", line 985: Where: While instantiating "std::__linear_insert<C*const*, C*>(C*const*, C*const*, C**)".
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm.cc", line 985: Where: Instantiated from std::__insertion_sort<C*const*>(C*const*, C*const*).
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm", line 811: Where: Instantiated from non-template code.
有人知道如何规避这个问题吗?当然,实际上我想将std::sort
与自定义比较函数一起使用,但即便是这个简单的版本也无效。
答案 0 :(得分:5)
看起来你的实际矢量是常量。它是const成员函数中访问的成员变量吗?它是一个const函数参数吗?
答案 1 :(得分:3)
#include <algorithm>
#include <vector>
struct C {};
int main()
{
std::vector<C*> v;
std::sort(v.begin(), v.end());
}
编译时没有错误
CC: Sun C++ 5.9 SunOS_sparc Patch 124863-19 2009/12/02
作为
调用CC lytenyn.cpp