c++11g++clang++lemon-graph-library

"no viable conversion" with lemon for clang but valid for g++


Currently, I try to incorporate lemon library in our project. Most developers are on Windows, they compile with MSVC, but I am in charge (for this part) to compile with gcc and clang.

I came across an error with clang that gcc does not reproduce and I managed to reduce the code:

#include <lemon/dfs.h>

int main() {
    lemon::ListDigraph g{};
    lemon::ListDigraph::Node node = g.nodeFromId(42);
    lemon::Dfs<lemon::ListDigraph> dfs{g};
    lemon::SimplePath<lemon::ListDigraph> path = dfs.path(node);
    return 0;
}  

With gcc, no errors.

/usr/bin/g++-5 -std=c++11 -Wall -O3  -I${SRC_ROOT}/external/lemon/latest -I${BIN_ROOT}/lemon -o ${TMP_ROOT}/core/src/core.cpp.o -c ${SRC_ROOT}/core/src/core.cpp

But with clang:

/usr/bin/clang++-3.7 -std=c++11 -Wall -stdlib=libc++ -O3 -I${SRC_ROOT}/external/lemon/latest -I${BIN_ROOT}/lemon -o ${TMP_ROOT}/core/src/core.cpp.o -c ${SRC_ROOT}/core/src/core.cpp

In file included from ${SRC_ROOT}/core/src/core.cpp:1:
In file included from ${SRC_ROOT}/external/lemon/latest/lemon/dfs.h:31:
${SRC_ROOT}/external/lemon/latest/lemon/path.h:408:23: error: no viable
      conversion from 'typename PredMapPath<ListDigraph, NodeMap<Arc> >::RevArcIt' to
      'lemon::ListDigraphBase::Arc'
        data[index] = it;;
                      ^~

Notes:

Questions :


Solution

  • It's a double dispatch related problem

    The code in http://lemon.cs.elte.hu/hg/lemon/file/9fd86ec2cb81/lemon/path.h#l443

    template <typename CPath>
    void buildRev(const CPath& path) {
        int len = path.length();
        data.resize(len);
        int index = len;
        for (typename CPath::RevArcIt it(path); it != INVALID; ++it) {
            --index;
            data[index] = it;; // sic!
        }
    }
    

    relies on this user-defined cast-operator of the iterator on the right hand side

    http://lemon.cs.elte.hu/hg/lemon/file/9fd86ec2cb81/lemon/bits/path_dump.h#l139

    operator const typename Digraph::Arc() const {
        return path->predMatrixMap(path->source, current);
    }
    

    but the left hand type of the assigment expression

    http://lemon.cs.elte.hu/hg/lemon/file/9fd86ec2cb81/lemon/list_graph.h#l89

    class Arc {
        friend class ListDigraphBase;
        friend class ListDigraph;
    protected:
        int id;
        explicit Arc(int pid) { id = pid;}
    public:
        Arc() {}
        Arc (Invalid) { id = -1; }
        bool operator==(const Arc& arc) const {return id == arc.id;}
        bool operator!=(const Arc& arc) const {return id != arc.id;}
        bool operator<(const Arc& arc) const {return id < arc.id;}
    };
    

    doesn't have a custom assignment operator, but a single-argument custom ctor that clang tries to match against conversions of the right side. And fails.

    Patching the right side of above shown lemon/path.h, line #443 with an simple explicit cast operator invocation

       data[index] =  it.operator const typename Digraph::Arc();; 
    

    makes the code at least compile with clang (3.5).

    The lemon-developers must decide whether this is desired behavior; a bug report should be filed for this.