无法再在std :: unique_ptr <derived>到std :: unique_ptr <base />之间转换

时间:2019-04-14 18:16:29

标签: c++ nlohmann-json

因此,在将一些代码添加到(反)序列化JSON之后,我在基类和派生类之间进行转换时遇到问题。我正在使用nlohmann JSON库。对于背景,这是常规的原始设置(在JSON代码之前):

async Task ChangeState(bool state)
{
    await Task.Yield();

    doStuffLock.EnterWriteLock();

    try
    {
        changeStateLock.EnterReadLock();

        try
        {
            await OutsideApi.ChangeState(state);
        }
        finally
        {
            changeStateLock.ExitReadLock();
        }
    }
    finally
    {
        doStuffLock.ExitWriteLock();
    }
}

async Task DoStuff()
{
    await Task.Yield();

    changeStateLock.EnterWriteLock();

    try
    {
        doStuffLock.EnterReadLock();

        try
        {
            await OutsideApi.DoStuff();
        }
        finally
        {
            doStuffLock.ExitReadLock();
        }
    }
    finally
    {
        changeStateLock.ExitWriteLock();
    }
}

在添加更改之前,以下声明有效:

class Base;
class Derived : public Base;

std::unique_ptr<Derived> Foo(Node x) {
  std::unique_ptr<Derived> result;
  /* Set some fields */
  return result;
}


std::unique_ptr<Base> NodeToBase(Node x) {
  std::unique_ptr<Base> result = nullptr;

  switch (x->type):
    case (SomeType):
      result = Foo(x);

  return result;
}

接下来,我将一些序列化函数添加到基类和派生类中,例如:

static_assert(std::is_convertible<Derived*, Base*>::value);
static_assert(std::is_convertible<std::unique_ptr<Derived>, std::unique_ptr<Base>>::value);

我还删除了class Base { virtual nlohmann::json ToJson() const; virtual void FromJson(const nlohmann::json &j); } class Derived : public Base { nlohmann::json ToJson() const override; void FromJson(const nlohmann::json &j) override; } const中成员变量上的所有Base量词,以允许反序列化。

现在,经过编译,上述Derived不再起作用,并且出现以下错误。

static_assert

任何帮助将不胜感激。我发现唯一可行的解​​决方法是替换:

foo.cpp:155:14: error: no viable overloaded '=' result = Foo(x); ~~~~~~ ^ ~~~~~~~ /usr/local/Cellar/llvm@6/6.0.1/include/c++/v1/memory:2347:28: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'unique_ptr<Derived, default_delete<Derived>>' to 'const unique_ptr<Base, default_delete<Base>>' for 1st argument class _LIBCPP_TEMPLATE_VIS unique_ptr { ^ /usr/local/Cellar/llvm@6/6.0.1/include/c++/v1/memory:2463:15: note: candidate function not viable: no known conversion from 'unique_ptr<Derived, default_delete<Derived>>' to 'unique_ptr<Base, default_delete<Base>>' for 1st argument unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT { ^ /usr/local/Cellar/llvm@6/6.0.1/include/c++/v1/memory:2555:15: note: candidate function not viable: no known conversion from 'std::unique_ptr<Derived>' to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument unique_ptr& operator=(nullptr_t) _NOEXCEPT { ^ /usr/local/Cellar/llvm@6/6.0.1/include/c++/v1/memory:2474:15: note: candidate template ignored: requirement 'is_convertible<typename unique_ptr<Derived, default_delete<Derived> >::pointer, pointer>::value' was not satisfied [with _Up = Derived, _Ep = std::__1::default_delete<Derived>] unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT { ^

result = Foo(x);

0 个答案:

没有答案