我有运行时错误,当使用std :: optional替换一些代码时:
旧代码:
T getValue();
...
const auto& value = getValue();
value.get();
Run Code Online (Sandbox Code Playgroud)
新代码:
std::optional<T> getValue();
...
const auto& value = getValue().value();
value.get(); // Runtime error, crash
Run Code Online (Sandbox Code Playgroud)
这对我来说无法预测.崩溃的原因是该方法返回T&&.
我的问题是在哪些情况下T&&可能有用,为什么该方法不返回a T.
完整代码:
#include <experimental/optional>
#include <iostream>
#include <memory>
struct Value {
std::unique_ptr<int> a = std::make_unique<int>(5);
};
std::experimental::optional<Value> getValue() {
Value v;
return v;
}
int main() {
const Value& value = getValue().value();
std::cout << *value.a << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) std::optional适合我的代码?我的代码包含许多看起来大致如下的函数:
bool GetName(_Out_ string& strRes)
{
// try to get/calculate the value
// value may also be empty
// return true on success, false otherwise.
}
Run Code Online (Sandbox Code Playgroud)
既然我已经找到了std::optional,我只需编写代码:
std::optional<std::string> GetName()
Run Code Online (Sandbox Code Playgroud)
std::optional?std::optional会带来性能价格,为了争论,让我们假设我愿意支付它。std::optional<int> iVal = 9;毫无意义。std::optional?我觉得std::optional很自然,它让我得出一个结论,默认情况下,我的许多本机类型bool, int, string, 应该声明为std::optional.
因此,而不是遵循以下前提:
std::optional仅在绝对需要时使用
我倾向于:
std::optional始终使用,除非您确定现在或将来不需要它。
以下规则是否有效:
使用std::optional时:
我正在做 Vulkan 教程 https://vulkan-tutorial.com/
#define GLFW_INCLUE_VULKAN
#include<GLFW/glfw3.h>
#include<optional>
struct s {
std::optional<uint32_t> num;//Intellisense Error
};
int main() {
return 5;
}
Run Code Online (Sandbox Code Playgroud)
我从一个空项目开始,并添加了包含和库;我可以在不包含 std::optional 的情况下编译和运行。
当我使用 std::optional 时,我得到 c2039 "optional is not a member of std"
我正在运行 Windows 10 和 VisualStudio 2019
这里发生了什么 ?
谢谢。
我有一个返回可选结构的方法,如下所示:
auto getBook(const std::string &title) const -> std::optional<Book>;
Run Code Online (Sandbox Code Playgroud)
我想在另一个返回可选作者的方法中调用此方法。问题在于,在调用方法之前,实现应该始终检查 getBook 返回的可选值是否已填充,如下所示:
auto getAuthor(const std::string &title) const -> std::optional<Author>
{
const auto optBook = getBook(title);
if (optBook.has_value)
return optBook->getAuthor();
else
return std::nullopt;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法以更短的方式编写它,以便如果填充了可选项,则调用该方法,但如果可选项为空,std::nullopt则返回。像这样的东西(我知道这目前不起作用,但你明白我的意思):
auto getAuthor(const std::string &title) const -> std::optional<Author>
{
return getBook(title).getAuthor();
}
Run Code Online (Sandbox Code Playgroud) 在std::optional::emplace 文档中有一个接受的重载std::initializer_list:
template< class U, class... Args >
T& emplace( std::initializer_list<U> ilist, Args&&... args );
Run Code Online (Sandbox Code Playgroud)
前提是
std::is_constructible<T, std::initializer_list&, Args&&...>::value 为真
我认为它可能用于放置 POD 类型,但显然这不是它的工作方式(在其他 SO 主题中解释说emplace函数使用()语法而不是{}):
struct A
{
int x;
int y;
int z;
};
int main()
{
A normalA{1, 2, 3}; // this is OK
std::cout << std::is_constructible<A, std::initializer_list<int>&, int, int, int>::value << std::endl; // false
std::cout << std::is_constructible<A, std::initializer_list<int>&>::value << std::endl; // false
std::optional<A> optA;
// optA.emplace({1, 2, …Run Code Online (Sandbox Code Playgroud) 我想我对如何存储可选值有点困惑。在构造包含std::optional<T>成员的类或结构时,这些成员是连续存储在内存中还是动态分配可选?例如,下面的结构是一个连续的内存块吗?
struct Material
{
std::string name;
std::optional<size_t> albedo;
std::optional<size_t> normal;
std::optional<size_t> metalness;
std::optional<size_t> roughness;
std::optional<size_t> ao; // ambient occlusion
bool hasAlphaChannel = false;
};
Run Code Online (Sandbox Code Playgroud) 我正在学习如何使用,但在将参数传递给函数时std::optional遇到问题,因为 Type 中包含 a ,这会阻止调用。std::optional<Type>std::unique_ptr
将这样的变量 ( std::optional<Type>) 传递给函数的正确方法是什么?
这是一个可以轻松重现该问题的代码片段:
\n#include <iostream>\n#include <optional>\n#include <memory>\nusing namespace std;\n\nstruct myStruct\n{\n std::unique_ptr<int> a;\n};\n\nint func(const myStruct& val, std::optional<myStruct> opt)\n{\n if (opt.has_value())\n {\n return *(opt.value().a);\n }\n else \n {\n return *(val.a);\n }\n}\n\nint main()\n{\n cout << "Hello World";\n myStruct val;\n val.a = std::make_unique<int>(5);\n std::optional<myStruct> opt = std::nullopt;\n myStruct temp;\n temp.a = std::make_unique<int>(10);\n opt = std::move(temp);\n std::cout << "result is " << func(val, opt) << std::endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n我在上面的代码中看到的错误: …
我尝试迭代std::optional:
for (auto x : optionalValue)
{
...
}
Run Code Online (Sandbox Code Playgroud)
预期如果为optionalValue空则不执行任何操作,但如果其中有值则执行一次迭代,就像它在 Haskell 中一样(可以说这很std::optional流行):
forM optionalValue
( \x ->
...
)
Run Code Online (Sandbox Code Playgroud)
为什么我不能迭代可选的?还有另一种更标准的 C++ 方法可以做到这一点吗?
这是一个愚蠢的问题,但我很好奇,但我还没有找到解释。建造 a 是否合法std::optional<std::nullopt_t>?如果你这样做了,你会怎样
std::optional<std::nullopt_t> x(std::nullopt);
Run Code Online (Sandbox Code Playgroud)
做?是否包含x值?
当 anint被本地声明(但未初始化或赋值)时,它的值是未定义的。当std::optional<int>在本地声明而没有显式初始化时,是否同样适用?它总是std::nullopt,还是具有未定义的值?