小编Tee*_*les的帖子

将 nullptr 转换为 std::span

我有一个const z* zs = nullptr;

\n

我想转换zsstd::span

\n

当我尝试这样做时,std::span<const z>(zs) 我收到一条错误消息

\n
\n

错误:没有匹配的函数可调用 \xe2\x80\x98std::span::span(const z* const&)\xe2\x80\x99

\n
\n

如何转换为 zsstd::span

\n

我试过std::span<const z>(zs[0])了,好像可以编译。这种方式正确吗?

\n

c++ nullptr std-span

5
推荐指数
2
解决办法
1319
查看次数

C++中0b1是什么意思?

我遇到了一段我无法理解的代码。

for (unsigned int i = (x & 0b1); i < x; i+= 2)
    {
        // body
    }
Run Code Online (Sandbox Code Playgroud)

这里,x是从0到5。

0b1是什么意思?例如:(0 & 0b1)、(4 & 0b1) 等的答案是什么?

c++

2
推荐指数
1
解决办法
2846
查看次数

为 std::可选对象的 std::array 赋值

我正在尝试填充 std::array 的 std::Optional 对象,如下所示。

\n
class MyClass\n{\n   private:\n    int distance;\n    MyClass(int x, int y);\n\n   friend class MasterClass;\n};\n\nMyClass::MyClass(int x, int y)\n{\n  distance = x+y;\n}\n\nclass MasterClass\n{\n  public:\n  MasterClass(std::array<std::optional<int>,5> xs, std::array<std::optional<int>,5> ys);\n  \n  private:\n  std::array<std::optional<MyClass>, 5> myclassarray{};\n\n};\n\nMasterClass::MasterClass(std::array<std::optional<int>,5> xs, std::array<std::optional<int>,5> ys)\n {\n     for(int i=0; i<5;i++)\n     {\n         myclassarray[i].emplace(new  MyClass(*xs[i], *ys[i])); //---(1)\n     }\n\n }\n
Run Code Online (Sandbox Code Playgroud)\n

从上面用 (1) 注释的行中,我收到以下错误,

\n
error: no matching function for call to std::optional<MyClass>::emplace(MyClass&)\n
Run Code Online (Sandbox Code Playgroud)\n

我也尝试用以下内容替换同一行

\n
 myclassarray[i] = new  MyClass(*xs[i], *ys[i]) ; //---(2)\n
Run Code Online (Sandbox Code Playgroud)\n

这会给我

\n
error: no match for \xe2\x80\x98operator=\xe2\x80\x99 …
Run Code Online (Sandbox Code Playgroud)

c++ stdarray c++17 stdoptional

1
推荐指数
1
解决办法
2082
查看次数

标签 统计

c++ ×3

c++17 ×1

nullptr ×1

std-span ×1

stdarray ×1

stdoptional ×1