Search code examples
c++unique-ptrdllexportpure-virtual

DLL Import of pure virtual struct and unique_ptr can not compile due to Compressed_pair


I have two projects where my code is in one and exports as a dll and the importing project is a test project. Ultimately I want a vtable that i can import into c as well using a struct only.

My pure virtual struct looks like this:

#ifdef DLL_EXPORTS
#define MyAPI  __declspec( dllexport )
#else DLL_EXPORTS
#define MyAPI  __declspec( dllimport  )
#endif DLL_EXPORTS

struct MyAPI IVirtual
{
    virtual int GetNumber() = 0;
    virtual ~IVirtual() = 0;
};

MyAPI IVirtual * IVirtual_New(int);
MyAPI void IVirtual_Delete(IVirtual *);

This has an Implementation for the dll:

#include "Virtual.h"

struct Concrete : IVirtual
{
    int n;
    Concrete(int n) : n(n) { }
    virtual int GetNumber() { return n; }
    virtual ~Concrete() {}
};

IVirtual::~IVirtual() {}

MyAPI IVirtual * IVirtual_New(int n)
{
    auto that = new Concrete(n);
    return that;
}

MyAPI void IVirtual_Delete(IVirtual * that)
{
    auto fingerscrossed = static_cast<Concrete*>(that);
    delete fingerscrossed;
}

The Test project imports the dll. The sharedpointer tests compiles and the assert is confirmed.

#include "CppUnitTest.h"
#include <memory>
#include <MyDLL/Virtual.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UsageTest
{       
    TEST_CLASS(UsageTest)
    {
    public:
        TEST_METHOD(SharedConstructor)
        {
            //looks ok - tests pass
            std::shared_ptr<IVirtual> sptr(IVirtual_New(4), IVirtual_Delete);
            Assert::AreEqual(4, sptr->GetNumber());
        }

        TEST_METHOD(UniqueConstructor)
        {
            std::unique_ptr<IVirtual, decltype(IVirtual_Delete)> uptr(IVirtual_New(6), IVirtual_Delete);
            Assert::AreEqual(6, uptr->GetNumber());
        }

    };
}

However when I add the UniqueConstructor method the test project stops to compile.

C2207: 'std::_Compressed_pair<_Ty1,_Ty2,false>::_Myval1': a member of a class template cannot acquire a function type

I assume this is related to the destructor but I was unable to identify what is really going on there.


Solution

  • Time for a break today.

    decltype(&IVirtual_Delete)