小编rm1*_*948的帖子

使用“重载”lambda 在函数中使用 std::array

我希望在 C++20 中执行以下操作:

template <class... Ts>
struct overloaded : Ts... {
    using Ts::operator( )...;
};

// Updated to work with C++17
#if (_cplusplus != 202002L)    // check for C++17 or C++20
// Deduction guide, google `CTAD for aggregates` for more info
template <typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;    // not needed before C++20
#endif

template <typename T, long int C = 0>
void emit(T const& data) {

    auto emit = overloaded {

        [&](const auto& value) {
            mOs << value;
        },
        [&](const …
Run Code Online (Sandbox Code Playgroud)

c++ lambda stdarray c++20

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

为什么在应调用派生类方法时调用基类方法

下面的代码eval在IMO应该调用NotGate版本时调用基类。它在R3调用中正确运行,但在R4调用中无效。

typeid调用时添加了许多调用,以标识实际的“门”,并出于某种原因查找门的类型NotGate在初始化期间更改为存储为抽象门的变量operator!operator*在我的完整代码中,或其他类似运算符不会发生这种情况。

#include <iostream>
using namespace std;
//---------------------------------------------------------------------------------------------------------------------
struct LGAbs {
    enum NodeState {
        Failure, Success, Running
    };
    LGAbs() {
    }
    virtual ~LGAbs() {
    }

    virtual NodeState eval();
};
//---------------------------------------------------------------------
LGAbs::NodeState LGAbs::eval() {
    return LGAbs::Failure;
}
//----------------------------------------------------------------------
struct TrueGate : public LGAbs {
    TrueGate() {
    }

    virtual LGAbs::NodeState eval() override {
        return Success;
    }
};
//----------------------------------------------------------------------
struct NotGate : public LGAbs {
    NotGate(LGAbs& g) …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c++ ×2

c++20 ×1

lambda ×1

stdarray ×1