samedi 27 février 2016

C++11 : Why is trying to store a pointer to function ambiguous

Here is my code:

    #include <functional>
    #include <iostream>
    #include<vector>

    using namespace std;

    // vector iterator
    template <class T> class vit
    {
            private:
            //vector<T>::iterator it;
            vector<T> m_v;
            function<bool (T, T)> m_fptr;
            int len, pos;
            public:
            vit(vector<T> &v) { this->m_v = v; len = v.size(); pos = 0;};
           //       it= v.begin(); };
            bool next(T &i) {
                    //if(it == m_v.end()) return false;
                    if(pos==len) return false;
                    //i = *it;
                    i = m_v[pos];
                    //if(idle) { idle = false ; return true; }
                    //it++;
                    pos++;
                    return true;};
            //bool idle = true;
            void set_same(function<bool (T,T)> fptr) { m_fptr = fptr ;};
            //void set_same(function<bool(int, int)> fun) { return ; }
            bool grp_begin() {
                    return pos == 0 || ! m_fptr(m_v[pos], m_v[pos-1]); };
            bool grp_end() {
                    return pos == len || ! m_fptr(m_v[pos], m_v[pos+1]); };
    };


    bool is_same(int a, int b) { return a == b; }

    main()
    {
            vector<int>  v ={ 1, 1, 2, 2, 2, 3, 1, 1, 1 };
            int total;
            for(auto it = v.begin(); it != v.end(); it++) {
                    if(it == v.begin() || *it != *(it-1)) {
                            total = 0;
                    }

                    total += *it;

                    if(it+1 == v.end() || *it != *(it+1)) {
                            cout << total << endl;
                    }
            }

            cout << "let's gry a group" <<endl;
            vit<int> g(v);
            int i;
            while(g.next(i)) { cout << i << endl; }

            cout << "now let's get really fancy" << endl;
            vit<int> a_vit(v);
            //auto is_same = [](int a, int b) { return a == b; };
            a_vit.set_same(is_same);
            //int total;
            while(a_vit.next(i)) {
                    if(a_vit.grp_begin()) total = 0;
                    total += i;
                    if(a_vit.grp_end()) cout << total << endl ;
            }
    }        

When I compile it with g++ -std=c++11 iter.cc -o iter, I get the result:

    iter.cc: In function 'int main()':
    iter.cc:63:17: error: reference to 'is_same' is ambiguous
      a_vit.set_same(is_same);
             ^
    iter.cc:37:6: note: candidates are: bool is_same(int, int)
     bool is_same(int a, int b) { return a == b; }
          ^
    In file included from /usr/include/c++/5.3.0/bits/move.h:57:0,
                     from /usr/include/c++/5.3.0/bits/stl_pair.h:59,
                     from /usr/include/c++/5.3.0/utility:70,
                     from /usr/include/c++/5.3.0/tuple:38,
                     from /usr/include/c++/5.3.0/functional:55,
                     from iter.cc:1:
    /usr/include/c++/5.3.0/type_traits:958:12: note:                         template<class, class> struct std::is_same
         struct is_same;
                ^

By way of explanation, I have created a class called 'vit'. It does two things: iterate over a vector, and determine if a new group has been reached.

The class function 'set_same' is supposed to store a function provided by the calling class to determine if two adjacent elements of a vector are in the same group. However, I can't seem to store the function in the class for future use by grp_begin() and grp_end() on account of the ostensible ambiguity of is_same.

What gives?

Aucun commentaire:

Enregistrer un commentaire