1

I want to write a matcher, which excludes classes containing incomplete types like: std::unique_ptr<ForwardDeclared>, but it is not identified as one, because it is a template type (I think so). Does anyone know how to circumvent this?

Minimal code:

// ...
class MoveDefaultToDecl: public MatchFinder::MatchCallback {
// ...

public:
    void run(const MatchFinder::MatchResult& r) override {
        // ...
        auto const* class_decl = r.Nodes.getNodeAs<CXXRecordDecl>("method-class");
        auto iter = std::find_if(class_decl->field_begin(), class_decl->field_end(),
                                 [](auto const& field) { return field->getType()->isIncompleteType(); });
        if (iter != class_decl->field_end()) {
            return;
        }
        // ...
    }
// ...
}

test.h

#pragma once

#include <memory>

class H {
    H();
    ~H() = delete;
};

class MyType;

class K {
    K();
    ~K();
    std::unique_ptr<MyType> ptr_;
};

test.cpp

#include "test.h"

class MyType {
    // But not here:
    MyType() = default;
    // But not here:
    ~MyType() = default;
    int i;
};

// CHECK: :[[@LINE+1]]:3: note: "xyz" binds here
H::H() = default;

// But not here
K::K() = default; // Actually binds here

// But not here
K::~K() = default; // Actually binds here

Fabian Keßler
  • 563
  • 3
  • 12
  • 1
    `std::unique_ptr` *is* a complete type—just one whose destructor mustn’t be instantiated until `Incomplete` is completed. – Davis Herring Apr 08 '21 at 13:43
  • You are right, now I just don't know how to correct that properly without reducing the readability. You can edit my question if you want. – Fabian Keßler Apr 08 '21 at 15:32
  • I don’t know what you mean to ask, so I can’t edit it to say what you mean. – Davis Herring Apr 08 '21 at 19:12
  • I want to move `= default` statements from the source to the header files. But I want to exclude those, which depend on the instantiation of used methods. For `std::unique_ptr` it's the destructor, but there are other types. So I want to prevent to move the instantiation of a method before the completion of its declaration. – Fabian Keßler Apr 14 '21 at 08:22
  • 1
    I don’t think there’s any way to tell in general which instantiations will need what functions (or what types to be complete). – Davis Herring Apr 14 '21 at 13:42

0 Answers0