Search code examples
c++boostg++shared-ptr

Never provide destructor in the PIMPL(using boost scoped_ptr), the g++(4.6.1) doesn't generate compile error, Why?


After I read the reference link: Must provide destructor in the PIMPL, I do the follow the example, but the g++(4.6.1) doesn't generate compile error as I expected

The source code is:

// Predeclare.h
#include <vector>
#include <boost/scoped_ptr.hpp>
#include <iostream>

struct A;

class Predeclare
{
    std::vector<A> alist_;
    boost::scoped_ptr<A> pa_;
    //A a;

public:
    Predeclare();
    //~Predeclare();

    void print();
    void set(int i);
};


// Predeclare.cpp
#include "Predeclare.h"

struct A
{
    int a;
};

Predeclare::Predeclare(): pa_(new A)
{}
/*
Predeclare::~Predeclare()
{}
*/

void Predeclare::print()
{
    std::cout << pa_->a << '\n';
}

void Predeclare::set(int i)
{
    pa_->a = i;
}

int main()
{
    Predeclare c1;
    c1.set(10);
    c1.print();

    return 0;
}

Then, compile the program

g++ Predeclare.cpp

Everything is ok, Why the g++(4.6.1) doesn't generate compile error?


Solution

  • In your example, the compiler only needs a destructor for Predeclare when it reaches the declaration of c1 in main(). By that point, A has already been defined, earlier in Predeclare.cpp.

    If you were to try to instantiate Predeclare in a different source file, you would likely run into problems.

    Note that instantiating std::vector<A> where A is incomplete yields undefined behavior. When you instantiate a container, the value type must be Destructible, and only complete types are Destructible.