这个问题中的代码使用了在https://github.com/LoopPerfect/neither找到的Either<>
实现。明确一下,我怀疑这不是这个特定库的问题,否则我会在那里创建一个issue。
下面的代码片段按预期工作:
std::future<std::string> f = std::async(std::launch::async, []()
{
return "test";
}
std::cout << f.get() << std::endl;
以下代码会产生分段错误:
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;
使用left(-1)
和neither::Either<int, int>
的left()
和right()
都可以起作用。我知道如果你多次调用std::future::get
可能会导致段错误,在这种情况下,在调用get
之前std::future::valid
将返回false,但是valid
会返回true。
这里有什么我不明白的吗?
e
构造正确,但this
不是。星号行调用了std::string::operator=(std::string const&)
,它需要存在一个std::string
- 但我们没有一个std::string
,我们只有未初始化的内存。 - Barrynew (&rightValue) R(e.rightValue)
。 - Barrystatic_cast<void*>
? - Barry::operator new(size_t, int*)
等函数并没有被保留? - T.C.struct L { void operator&(); };
可以翻译为:定义了一个名为L的结构体,其中包含一个名为operator&的函数。 - Barry