Std :: future <neither :: Either <int, std :: string >> segmentation error

This question uses the implementation Either<>found here: https://github.com/LoopPerfect/neither . To be clear, I doubt that this is a problem with this particular library, otherwise I would create a problem there.

The following snippet works as expected:

std::future<std::string> f = std::async(std::launch::async, []()
{
    return "test";
}

std::cout << f.get() << std::endl;

And the following generates a segmentation error:

std::future<neither::Either<int, std::string>> f = std::async(std::launch::async, []()
{
    return neither::Either<int, std::string>(neither::right(std::string("test")));
});

neither::Either<int, std::string> v = f.get(); // Segmentation fault
std::cout << v.right().value << std::endl;

Refund left(-1)works, as neither::Either<int, int>for left()and right(). I know what std::future::getsegfault can generate - you called it twice, and in this case it std::future::validreturns false just before the call get, but it validreturns true.

Am I missing something here?

+4
1

- ?

. , :

constexpr Either( Either<L, R> const& e )
  : isLeft(e.isLeft) {
  if(isLeft) {
    leftValue = e.leftValue;
  } else {
    rightValue = e.rightValue; // (*)
  }
}

this->rightValue , std::string - .

:

Either(Either<L, R> const& e)
  : isLeft(e.isLeft)
{
  if(isLeft) {
    new (&leftValue) L(e.leftValue);
  } else {
    new (&rightValue) R(e.rightValue);
  }
}

, , , :

::new (static_cast<void*>(std::addressof(leftValue))) L(e.leftValue);
+11

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


All Articles