如果一个类T具有对齐要求,例如由alignas关键字指定的对齐要求,std::optional<T>并且boost::optional<T>保证尊重所述对齐?
如果它们只是T对象和a的包装类bool initialized,那么它们会T根据需要自动对齐它们的成员,但标准和boost文档声明它们不能容纳任何对象并且很好地处理昂贵的构造对象.据我所知,他们不仅仅包含一个T.相反,它们似乎分配了一个缓冲区,T手动构造或销毁它.因此,C++语言不会自动对齐缓冲区,因为它不是类型T.
那么,做std::optional<T>并boost::optional<T>正确对齐他们的托管T对象?他们是否也提供optional<T>::operator new并optional<T>::operator new[]尊重对齐要求?
为什么我不能用nullptr而不是nullopt?为什么要{}翻译nullopt?
std::optional 可以使用语法来访问其值,类似于普通指针一样。
std::optional<string> some_str;
if (some_str)
(*some_str).c_str();
Run Code Online (Sandbox Code Playgroud)
但它也有两个功能,has_value()并value()提供访问其价值和检查,如果该值存在。
std::optional<string> some_str;
if (some_str.has_value())
some_str.value().c_str();
Run Code Online (Sandbox Code Playgroud)
我想知道这两者之间有什么区别?
1.更冗长
2.性能?
3.更好的日志记录和调试?value()会抛出异常。
此代码不能使用命令 g++ -std=c++17 main.cpp 编译
#include <iostream>
#include <experimental/optional>
int main()
{
std::optional<int> x;
std::cout << "Hello World";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误如下:
有没有办法让这段代码编译?
出于调试目的,我正在编写一个函数,该函数迭代任何类型的可选变量向量以检查哪些变量已初始化,但对has_value()所有变量的检查都返回了true,尽管尚未为其中某些变量分配任何值。
我很感激任何帮助指出我的误解,因为我是 C++ 新手。代码如下。请注意,当注释行被取消注释时,if 语句会发现该变量没有值。
#include <iostream>
#include <optional>
#include <any>
bool SimpleCheck(std::vector<std::optional<std::any>> toCheck)
{
bool res = false;
for (int i = 0; i < toCheck.size(); ++i)
{
// toCheck[i] = std::nullopt;
if (!toCheck[i].has_value())
{
std::cout << "item at index " << i << " had no value\n";
res = true;
}
}
return res;
}
int main()
{
std::optional<int> i = 5;
std::optional<std::string> str;
std::optional<double> doub = std::nullopt;
bool check = SimpleCheck({i, str, doub}); …Run Code Online (Sandbox Code Playgroud) class ScopedClass {
public:
ScopedClass() {
cout << "constructor called\n";
}
~ScopedClass() {
cout << "destructor called\n";
}
ScopedClass(const ScopedClass &t)
{
cout << "copy constructor called\n";
}
ScopedClass(ScopedClass &&t)
{
cout << "move constructor called\n";
}
}
std::optional<ScopedClass> check_and_return(bool condition) {
if (condition == true)
return std::make_optional(ScopedClass());
return std::nullopt;
}
int main() {
auto x = check_and_return(true);
}
Run Code Online (Sandbox Code Playgroud)
我想要一个ScopedClass对象,但当ScopedClass()我将其包含在std::optional. 这意味着析构函数在复制/移动时被调用,并且被调用两次。一旦进入check_and_return,一次main结束时。
我希望仅在结束时调用析构函数main。
有办法实现这一点吗?或者不使用某种替代方法std::optional?
我正在尝试 C++17 功能std::optional
可选的返回类型是std::optional<std::pair<int, int>>. 我在函数中调用该
sum_pair函数print_answer并想要一个可选的打印。
在print_answer函数中,我想检查所需的对是否包含要显示的内容。就像下面给出的示例一样:可选返回工厂函数可用作 while 和 if 的条件
以下是代码:这里有错误
#include <iostream>
#include <vector>
#include <unordered_map>
#include <optional>
typedef std::optional<std::pair<int, int>> returnType;
// following algorithum works fine: just to show,
// how I have used the std::optional
returnType sum_pair(const std::vector<int>& vec, const int sum)
{
std::unordered_map<int, int> compIndexMap;
int index = 0;
for(const int& ele: vec)
{
if(auto check = compIndexMap.find(sum - ele); check != compIndexMap.cend())
return …Run Code Online (Sandbox Code Playgroud) 以下代码会导致编译器错误:
#include <optional>
class A {
};
class B {
private:
const A a;
};
int main()
{
B b;
std::optional<B> bo1;
bo1 = b;
}
Run Code Online (Sandbox Code Playgroud)
例如,在 gcc 上,错误显示为:
#include <optional>
class A {
};
class B {
private:
const A a;
};
int main()
{
B b;
std::optional<B> bo1;
bo1 = b;
}
Run Code Online (Sandbox Code Playgroud)
在 MSVC 上,作为另一个例子,错误显示:
main.cpp: In function 'int main()':
main.cpp:12:7: error: uninitialized const member in 'class B'
12 | B b;
| ^
main.cpp:7:13: note: 'const A …Run Code Online (Sandbox Code Playgroud) 如何返回一个类似于 null 的值std::optional?我的函数接收一个 int 索引作为参数来遍历列表,如果索引值无效,如何std::optional使用它返回类似于 null 的值?
这是源代码:
using namespace std;
class obj {
public:
obj() = default;
obj(int i) : i_{i} {}
int I() const {return i_;}
int const & rI() const {return i_;}
void I(int i) {i_ = i;}
void show() const {cout << "addr = " << this << ", i_ = " << I() << endl;}
private:
int i_{0};
};
struct {
optional<obj> o_0 {nullopt};
optional<obj> o_1 {nullopt};
optional<obj> o_2 {nullopt};
void set_o0(obj o_) {o_0 = o_;}
void set_o1(obj o_) {o_1 = o_;} …Run Code Online (Sandbox Code Playgroud) 如果我想返回std :: nullopt作为非恒定引用,请形成学术观点。我将如何做同样的事情。
没什么背景,今天当我在处理返回std :: optional>引用的代码时,却忘记了使返回常量,并得到了错误。
Run Code Online (Sandbox Code Playgroud)Error (active) E0434 a reference of type "std::optional<std::vector<std::any, std::allocator<std::any>>> &" (not const-qualified) cannot be initialized with a value of type "const std::nullopt_t" Socket.IO D:\Hardware\Windows\Visual Studio\Socket.IO\Socket.IO\main.cpp 46 Error C2440 'return': cannot convert from 'const std::nullopt_t' to 'std::optional<std::vector<std::any,std::allocator<_Ty>>> &' Socket.IO d:\hardware\windows\visual studio\socket.io\socket.io\main.cpp 46
只是想知道是否有人想使用std :: optional返回一个非常量引用,他将如何做。
使用平台:Windows 10 Pro x64
开发环境:Visual Studios 15.9.9
std::vector<int>> a;
std::optional<std::vector<int>>& test(int b)
{
a.clear();
a.push_back(b);
if(b)
return a;
else
return std::nullopt;
}
Run Code Online (Sandbox Code Playgroud) c++ ×11
stdoptional ×11
c++17 ×7
std ×3
boost ×1
if-statement ×1
pointers ×1
stdany ×1
stdtuple ×1