an i...">

They include a class that has std :: unique_ptr <T> as a field, and "T" is an incomplete type

I created a tiny test case for std::unique<B>an incomplete type B.

test.h

#pragma once
#include <memory>
class B;   //<--- compile error here
class Test{
    std::unique_ptr<B> bPtr;   
    //#1 need to move destructor implementation to .cpp
    public: ~Test();
};

test.cpp

#include "Test.h"
class B{};
Test::~Test(){}  //move here because it need complete type of B

main.cpp

#include <iostream>
#include "Test.h"
using namespace std;
int main(){
   Test test;
   return 0;
}

I got this error: -

/usr/include/++/4.8.2/bits/unique_ptr.h: 65: 22: error: invalid application of "sizeof" to incomplete type "B"

As far as I understand, the compiler tells me what Bis an incomplete type (in main.cpp), so it cannot delete correctly B.

However, in my design, I want to main.cpp not have a full type B.
Very roughly, this is a pimple.

Is there a good workaround?

Here are a few similar questions, but none of them offer a clean workaround.

std::unique_ptr ?
unique_ptr? #3 .

Edit:

Kerrek SB . !

, : -
unique_ptr, ?
? - , , .

+4

Source: https://habr.com/ru/post/1674081/


All Articles