小编ril*_*man的帖子

C++ 从 std::vector<std::function<...>> 中删除 std::function

我有以下回调系统

class ... {

    ...

    std::vector<std::function<void()>>        systemBringUps;
    std::vector<std::function<void()>>        systemTearDowns;
    std::vector<std::function<void(EntityID_t)>> systemMains;
    std::vector<std::function<bool(EntityID_t)>> systemChecks;

    template <typename T>
    void registerSystem() {
        systemBringUps.push_back(T::systemBringUp);
        systemTearDowns.push_back(T::systemTearDown);
        systemMains.push_back(T::systemMain);
        systemChecks.push_back(T::systemCheck);
        T::onSystemRegister();
    }

    template <typename T>
    void deregisterSystem() {
        std::function<void()> bringUp = T::systemBringUp;
        std::function<void()> tearDown = T::systemTearDown;
        std::function<void(EntityID_t)> smain = T::systemMain;
        std::function<bool(EntityID_t)> check = T::systemCheck;

        std::remove(systemBringUps.begin(), systemBringUps.end(), bringUp);
        std::remove(systemTearDowns.begin(), systemTearDowns.end(), tearDown);
        std::remove(systemMains.begin(), systemMains.end(), smain);
        std::remove(systemChecks.begin(), systemChecks.end(), check);
        T::onSystemDeregister();
    }
Run Code Online (Sandbox Code Playgroud)

registerSystem模板功能工作正常,但deregisterSystem功能无法编译,说明

...

/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/predefined_ops.h:241:17: error: invalid operands to binary expression
      ('std::function<bool (unsigned short)>' and 'const std::function<bool …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

6
推荐指数
2
解决办法
102
查看次数

非类型变量参数错误

我试图take在Haskell中编写我自己的函数版本,我不明白我哪里出错了.这是我的代码:

take' :: (Num i, Ord i) => i -> [a] -> [a]
take' n xs
    | n <= 0  = []
    | null xs = []
    | otherwise = first : rest 
    where first = head xs
          rest  = take' (n - 1) (tail xs)
Run Code Online (Sandbox Code Playgroud)

据我所知,(Num i, Ord i) => i ...在函数的开头指定意味着我应该能够传递一个负整数.但是当我尝试take' -1 [1..10]使用交互式解释器时,我收到此错误:

*Main> take' -1 [1..10]

<interactive>:154:1: error:
    * Non type-variable argument
        in the constraint: Num (i -> [a2] -> [a2]) …
Run Code Online (Sandbox Code Playgroud)

haskell

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

标签 统计

c++ ×1

c++11 ×1

haskell ×1