[llvm-bugs] [Bug 31270] New: Pure virtual function called

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Dec 5 09:13:24 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=31270

            Bug ID: 31270
           Summary: Pure virtual function called
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Frontend
          Assignee: unassignedclangbugs at nondot.org
          Reporter: marco.diiga at gmail.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

It seems that both MSVC and gcc reject this code by detecting the abstract
class instantiation while clang 4.0 compiles it and runs to a "Pure virtual
function called" error:

#include <vector>
#include <iostream>
using namespace std;

template <typename P>
class Surface {
public:
    virtual int distance(const P& from, const P& to) const = 0;
    virtual bool check(const vector<P>& path, const P& from, const P& to) const
= 0;
    virtual vector<P> lookAround(const P& at) const = 0;
};

class PlanarSurface : public Surface<pair<int, int>> {
public:
    using point_type = pair<int, int>;
    int distance(const point_type&, const point_type&) const override {
        return 42;
    }

    bool check(const vector<point_type>&, 
               const point_type&, 
               const point_type&) const override {
        return true; // there would be the check
    }

    vector<point_type> lookAround(const point_type&) const override {
        vector<point_type> result;
        //...
        return result;
    }
};

template <typename P>
class Robot {
public:
    Robot(const Surface<P>& s): surface(s) {}
    vector<P> findPath(const P& from, const P& to) {
        auto path = searchPath(from, to);
        if (surface.check(path, from, to)) {
            return path;
        }
        throw runtime_error("path not found or incorrect");
    }
private:
    virtual vector<P> searchPath(const P& from, const P& to) = 0;
protected:
    const Surface<P>& surface;
};

template <typename P>
class MyRobot: public Robot<P> {
public:
    MyRobot(Surface<P> m): Robot<P>(m) {}
private:
    vector<P> searchPath(const P& from, const P& to) override {
        vector<P> result;
        // ...
        // use one of surface's virtual methods
        auto dist = this->surface.distance(from, to); // Pure virtual function
called!
        cout << dist << endl;
        // ...
        return result;
    }
};

int main() {
    PlanarSurface plane;
    MyRobot<pair<int, int>> robot(plane);
    robot.findPath({1,2}, {3,4});
    return 0;
}

(http://melpon.org/wandbox/permlink/6bsWOwweJTRzMwLA)

Not sure if the diagnostic is mandatory but it would surely be a nice-to-have
feature.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20161205/acecc2b5/attachment.html>


More information about the llvm-bugs mailing list