开发者

Borland Warning 8092

Regarding the following C++ code,

LengthAlphabeticalSort lengthAlphabeticalSort;
outputList.sort(lengthAlphabeticalSort); // causes borland 开发者_Python百科8092 error, guaranteed stable_sort.

class LengthAlphabeticalSort
{
    public:
        bool operator() (std::string str1, std::string str2)
        {
            if(str1.length() < str2.length())
                return true;
            else
                return false;
        }
};

I get a warning when compiling with the borland compiler:

Warning W8092 worder.cpp 138: template argument _Pr3 passed to 'sort' is not an iterator: random iterator required in function Worder::CommonWords(const Worder &) const Turbo Incremental Link 5.69 Copyright (c) 1997-2005 Borland

Can anybody tell me how to fix this problem? It compiles cleanly with VS2010 and gnu


Well, the sort member function on a std::list does take a binary functor like yours, so from looking at your posted code, I'd say your compiler is wrong.

However, the error message you posted puzzles me:

random iterator required in function Worder::CommonWords(const Worder &) const

Why is it saying that a random iterator is required in CommonWords? Is CommonWords the function from which sort is called?


Personally, I find the name LengthAlphabeticalSort confusing because the sort is purely by length, not by something alphabetical. Also, you should pass strings by reference-to-const, and the if-else is superfluous:

struct ByLength
{
    bool operator()(const std::string& a, const std::string& b)
    {
        return a.length() < b.length();
    }
};

The initial list is guaranteed to be in alphabetical order.

Does that mean you are sorting twice, first alphabetical, then by length? I think it's better practice to sort only once with an adequate predicate:

struct FirstByLengthThenAlphabetical
{
    bool operator()(const std::string& a, const std::string& b)
    {
        if (a.length() < b.length()) return true;
        if (b.length() < a.length()) return false;
        return a < b;
    }
};


Recently, I came across this problem while doing some compilation under an older version of Borland as well (this issue may be fixed in more recent versions). I was able to experiment and discover that it seems Borland does a lookup by function name to see if you might be calling an stl algorithm. Because of this, anything named exactly the same as an stl algorithm will check to see if you are passing the proper iterator type into the function.

This is messy, because sort is std::sort(RandomAccessIterator first, RandomAccessIterator last) algorithm, but sort is also a std::list::sort(Compare comp) method. Unfortunately, this strikes me as a very egregious compiler error, that is entirely dependent on the name of the function/method.

I don't know what the workaround would be for std::list or other containers that have methods named the same as algorithms, but the best solution would be to ignore the warning for that particular issue by using the command-line option: -w-8092

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜