[LLVMbugs] [Bug 19085] New: Can't std::sort a collection of std::pair if one pair element is const

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sat Mar 8 15:14:11 PST 2014


http://llvm.org/bugs/show_bug.cgi?id=19085

            Bug ID: 19085
           Summary: Can't std::sort a collection of std::pair if one pair
                    element is const
           Product: libc++
           Version: 3.3
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: hhinnant at apple.com
          Reporter: james at weatherley.net
                CC: llvmbugs at cs.uiuc.edu, mclow.lists at gmail.com
    Classification: Unclassified

#include <iostream>
#include <vector>

int main(int argc, const char * argv[])
{
    // Replace the std::vector declaration with the commented out one and it
builds.
    //
    //std::vector<std::pair<int, int>> vec = {
    std::vector<std::pair<const int, int>> vec = {
        std::make_pair(9, 8),
        std::make_pair(7, 6),
        std::make_pair(5, 4),
        std::make_pair(3, 2)
    };

    std::sort(vec.begin(), vec.end(), [](const std::pair<const int, int>& p1,
const std::pair<const int, int>&p2) {
        return true;
    });

    for(auto pair:vec)
    {
        std::cout << pair.first    << " : " << pair.second    << "\n";
    }
}

Build the above and you get errors:

/Applications/Xcode5.0/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/utility:292:15:
Read-only variable is not assignable

/Applications/Xcode5.0/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++/v1/utility:255:15:
Read-only variable is not assignable

I'm sorting - why does it want to assign to the const int pair element? Change
the pair declaration to a pair of non-const ints and it builds just fine.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20140308/056b0b88/attachment.html>


More information about the llvm-bugs mailing list