[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


            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 {
    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>> {
    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 {
    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");
    virtual vector<P> searchPath(const P& from, const P& to) = 0;
    const Surface<P>& surface;

template <typename P>
class MyRobot: public Robot<P> {
    MyRobot(Surface<P> m): Robot<P>(m) {}
    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
        cout << dist << endl;
        // ...
        return result;

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


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

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