samedi 23 avril 2016

Array of std::set and comparator

I'm trying to figure out why this code doesn't compile when you access the array of std::set named asdf. Without the function getItem the code compiles, however, if you try and access an element of asdf such as index 0 in this example. The compiler will throw this error.

main.cpp(21): error C2440: 'return': cannot convert from 'const std::set<test *,test::compare,std::allocator<_Kty>>' to 'const std::set<test *,std::less<_Kty>,std::allocator<_Kty>> &'
      with
      [
          _Kty=test *
      ]
main.cpp(21): note: Reason: cannot convert from 'const std::set<test *,test::compare,std::allocator<_Kty>>' to 'const std::set<test *,std::less<_Kty>,std::allocator<_Kty>>'
      with
      [
          _Kty=test *
      ]
main.cpp(21): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Here is the example:

#include <set>

struct test {
    int idx;
    struct compare {
        bool operator()(const test* a, const test* b) {
            return a->idx < b->idx;
        }
    };
};


class asdf123 {
public:
    asdf123() {

    }

    const std::set<test*>& getItem() const
    {
        return asdf[0];
    }

private:
    typedef std::set<test*, test::compare> stuffType[100];
    stuffType asdf;
};

int main() {
    asdf123 a;
    return 0;
}

Without the comparator the code works fine.

Aucun commentaire:

Enregistrer un commentaire