相关疑难解决方法(0)

在C++中的懒惰评估

C++没有对延迟评估的本机支持(如Haskell所做的那样).

我想知道是否有可能以合理的方式在C++中实现延迟评估.如果是的话,你会怎么做?

编辑:我喜欢Konrad Rudolph的回答.

我想知道是否可以以更通用的方式实现它,例如通过使用参数化类lazy,它基本上适用于矩阵矩阵的矩阵.

对T的任何操作都会返回惰性.唯一的问题是将参数和操作代码存储在惰性本身中.任何人都可以看到如何改善这一点?

c++ lazy-evaluation

58
推荐指数
4
解决办法
4万
查看次数

C++如何将参数包存储为变量

目前我在尝试存储参数包时遇到问题,这是设计的示例代码:

template<typename Func, typename... Args>
void handleFunc(Func func, Args&&... args) {
    struct nest {
        Func nestFunc;
        Args... nestArgs; // I DONT KNOW WHAT TO DO HERE
        void setup(Func func, Args... args) {
            nestFunc = func;
            nestArgs = (args)...; // SO I CAN SET IT HERE
        }
        // Later I will forward this and run the function with its arguments
        unsigned process() {
            nestFunc(std::forward<Args>(nestArgs)...); // USE IT HERE
            return 0;
        }
    };
    nest* myNest;
    myNest->setup(func, (args)...);
}
Run Code Online (Sandbox Code Playgroud)

这是问题所涉及的所有内容的示例,我需要存储参数以便稍后在我的nest结构中调用.此外,如果你有一个解决方案来存储它,但设置它与我的不同,也请让我知道这一点.谢谢.

c++

21
推荐指数
2
解决办法
7519
查看次数

绑定可变参数模板参数时的副本太多

我正在创建一个工作队列.作业将在线程A中创建,然后作业将发送到线程B,线程B将完成作业.作业完成后,作业将被发送回线程A.

#include <functional>
#include <iostream>
#include <memory>

using namespace std;

template<typename T, typename... Args>
class Job
{
    public:
        Job(std::weak_ptr<T> &&wp, std::function<void(const Args&...)> &&cb)
            : _cb(std::move(cb)), 
              _cbWithArgs(), 
              _owner(std::move(wp)) {}

    public:
        template<typename... RfTs>
        void bind(RfTs&&... args)
        {
            // bind will copy args for three times.
            _cbWithArgs = std::bind(_cb, std::forward<RfTs>(args)...);
        }

        void fire()
        {
            auto sp = _owner.lock();
            if (sp)
            {
                _cbWithArgs();
            }
        }

    private:
        std::function<void(const Args& ...)> _cb;
        std::function<void()> _cbWithArgs;
        std::weak_ptr<T> _owner;
};

struct Args
{
    Args() = default;
    Args(const Args &args) …
Run Code Online (Sandbox Code Playgroud)

c++ templates bind variadic-templates c++11

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

如何将可变参数模板参数绑定到函数

我正在尝试模仿 std::thread 构造函数功能:

template< class Function, class... Args > 
explicit thread( Function&& f, Args&&... args );
Run Code Online (Sandbox Code Playgroud)

我试过使用调试器来查看它是如何工作的,但我无法弄清楚。

如何像线程的构造函数那样创建和存储绑定类型?

像这样(语法可能错误):

class myClass{
private:
auto bindType;

public:
template< class Function, class... Args > 
explicit myClass( Function&& f, Args&&... args ) : bindType(somehowBind(f, args) {}
void evaluate() {bindType();}
};
Run Code Online (Sandbox Code Playgroud)

用法示例:

int test(int i) {return i;}

int main(){
myClass my(test, 5);
my.evaluate();
}
Run Code Online (Sandbox Code Playgroud)

请注意,我不在乎somehowBind函数是否会忽略返回类型,即它的返回类型可以是 std::function 之类的东西。我不想做的就是了解我如何绑定class... Args到给定的函数f,以便在调用后somehowBind它会像 std::bind 一样。为了澄清我的观点,您可以考虑我想要实现的目标如下:

thread t(test, 5); // unlike the usual …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

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