jeudi 28 mai 2015

Using comparison function for the key type for sets results in runtime error

I've read this question and it does not help me.

My question is: Why am I getting a runtime error when using a comparison function for the key type for set as below?

multiset<Phone, decltype(comp)*> phones { Phone(911), Phone(112) };
              //^^^^^^^^^^^^^^^

In VC2013 it gives me this for the above code:

Unhandled exception at 0x73DECB49 in debugit.exe: 0xC0000005: Access violation executing location 0x00000000.

Here is a small example that produces the error:

#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;

struct Phone {
    Phone(long long const &num) : number{num} {}
    long long number;
};

// compare function:
bool comp(Phone const &n1, Phone const &n2) { return n1.number < n2.number; }

int main()
{   // The below line produces the runtime error.
    multiset<Phone, decltype(comp)*> phones { Phone(911), Phone(112) };
}

I can't see what I'm doing wrong here. I've compiled with VC2013 and g++ (GCC) 4.9.1 both result in the same.

Aucun commentaire:

Enregistrer un commentaire