CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Transferring items from an unsorted set to a sorted set

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,721

    Transferring items from an unsorted set to a sorted set

    This code builds fine with gcc and it used to build fine with VC2008 but it causes a compiler error with VS2019. The original code used more complicated object types so the comparisons look quite clumsy for a simple int - however the error is still the same:-

    Code:
    #include <set>
    
    struct compare_fn
    {
    	bool operator()(const int* p1, const int* p2)
    	{
    		return *p1 < *p2;
    	}
    };
    
    typedef std::set<int* > OrigList;
    #if 0
    typedef std::set<int*, compare_fn> SortedList;
    #else
    typedef std::set<int* > SortedList;
    #endif
    
    void
    create_sorted_list(const OrigList& orig_list, SortedList& sorted_list)
    {
    	for (OrigList::const_iterator i = orig_list.begin(); i != orig_list.end(); ++i) {
    		sorted_list.insert(*i); // <--- ERROR AT THIS LINE !!
    	}
    }
    As the code stands, it compiles fine - but that's only because the definitions for OrigList and SortedList are identical. So try changing #if 0 to be #if 1

    What's supposed to happen is that sorted_list.insert(*i); copies objects into a new set via in a call to compare_fn - and apparently this should work - but VS2019 gives a (slightly strange) compiler error:-

    Code:
    error C3848: expression having type 'const compare_fn' would lose some const-volatile qualifiers in order to call 'bool compare_fn::operator ()(const int *,const int *)'
    Can anyone suggest a way to make this work? At one stage I tried removing all the const modifiers but IIRC that just produced a different error.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,463

    Re: Transferring items from an unsorted set to a sorted set

    operator() needs to be declared const

    Code:
    bool operator()(const int* p1, const int* p2) const
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2019 (16.9.3)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,721

    Re: Transferring items from an unsorted set to a sorted set

    Woohoo!! Thanks 2kaud - I'd been scratching my head for over a day and didn't notice that !!
    "A problem well stated is a problem half solved. - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)