小编Ale*_*nik的帖子

std ::具有不同参数的函数的集合

我正在尝试编写一个简单的调度程序,用户代码可以将回调附加到它.每个事件都有一个已知的签名,用户代码需要使用正确的数字和参数类型调用dispatch.这由可变参数管理.但是,freestandingInt不被接受,因为向量不是正确的类型.如何使它通用?

遵循一个最小的例子

void freestanding() {
 std::cout << "freestanding" << std::endl;
}

void freestandingInt(int iArg) {
  std::cout << "freestandingInt " << iArg << std::endl;
}


struct Dispatcher {
 typedef struct Event_ {
   std::vector<std::function<void()> > listeners;
 } Event;

 template<class... Args>
 void dispatch(int eventNr, Args&&... args) {
   for (auto listener: events[eventNr].listeners) {
     std::function<void()> f(std::bind(listener, std::forward<Args>(args)...));
     f();
   }
 }

 std::map<int, Event> events;
};

int main (int argc, char **argv) {
  Dispatcher disp;
  disp.events[0].listeners.push_back(freestanding);
  disp.dispatch(0); // OK

  // error here
  //disp.events[1].listeners.push_back(freestandingInt);

}
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11 std-function

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

使用相同的构造函数参数初始化所有元素或 std::array

我想知道是否可以std::array使用隐式删除的默认构造函数初始化对象,而不先知道数组的大小,因为它是模板参数,因此失去了使用初始化列表的可能性。代码如下,它以打破“调用的隐式删除默认的构造函数std::array<A, 3UL>

struct A {
  A (int b, int c) : mb(b), mc(c) { }
  int mb;
  int mc;
};

template <size_t NR_A>
struct B {
  B (int b, int c) : 
    // <- how to initialize mAs here?
  { }
  std::array<A, NR_A> mAs;
};

B<3> inst(1,1);
Run Code Online (Sandbox Code Playgroud)

编辑:我想所有的初始化A的的mAsA{1,1}

c++ templates

7
推荐指数
2
解决办法
1140
查看次数

使用sed将文本文件转换为C字符串

我想使用sed替换文本文件中的换行符,制表符,引号和反斜杠,以便在C中将其用作char常量,但我在开始时就丢失了.在输出中维护换行符会很好,添加一个'\n',然后是双引号来关闭文本行,一个crlf,另一个双引号来重新打开该行,例如:

一号线

2号线

会成为

"一号线\n"

"2号线\n"

任何人都可以至少指出我正确的方向吗?谢谢

c c++ sed

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

模板参数扣除政策

我有一个类应该用于将运算符应用于标量或向量以及这些的混合2.该类有效,但我必须手动传递模板参数,而我希望它们从构造函数中扣除.代码如下:

template <template <class,class> class OP, typename Type1, typename Type2>
class BinaryOperator :
    public OP <typename Type1::ReturnType, typename Type2::ReturnType> {
public:

    BinaryOperator(Type1* arg1_, Type2* arg2_) :
        m_arg1(arg1_),
        m_arg2(arg2_) {

    }

    ReturnType evaluate() {
        return this->apply(m_arg1->evaluate(), m_arg2->evaluate());
    }

private:
    Type1* m_arg1;
    Type2* m_arg2;
};
Run Code Online (Sandbox Code Playgroud)

现在,OP是一个具有完整模板专用的结构,用于Scalar/Vector的各种组合,它们是2 ctor参数的ReturnTypes.我必须明确写

new BinaryOperator<op_adder, Axis, Constant>(a1, c2)
Run Code Online (Sandbox Code Playgroud)

虽然我只想写

new BinaryOperator<op_adder>(a1, c2)
Run Code Online (Sandbox Code Playgroud)

并扣除Axis和Constant.编译器错误是:

 too few template arguments for class template 'BinaryOperator'
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?我做错了什么?谢谢亚历克斯

c++ templates

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

指向具有不同参数的成员函数的指针的容器

我到处寻找(Modern C++ design&co),但我找不到一种很好的方法来存储一组接受不同参数并在不同类上运行的回调.我需要这个,因为我希望我的应用程序的每个对象都有可能将其中一个方法的执行推迟到一个主Clock对象,跟踪当前时间,可以在正确的时刻调用这些方法.我的目标代码是:

void executeAction1Deferred(int time, int arg1, bool arg2)方法中class1,时间是将来需要的执行时间,应该是这样的:

Clock::defer(time, Class1::action1, this, arg1, arg2);
Run Code Online (Sandbox Code Playgroud)

Clock::defer(??? what signature ????)表示此任务的对象中,存储在优先级队列中,其中时间是密钥.对于每个Clock量程,然后遍历任务列表,并且将执行需要在该量程中运行的任务.请注意,我使用"defer"作为静态函数,因为我打算Clock使用单例的对象,但它也可以是成员函数,它只是选择的问题.

我曾经想过void*用来保存一个可变数量的参数,但让我的action1()方法接受一个void*非常可怕,也因为每次我直接使用这个函数时我都需要为参数创建一个结构而不推迟它.

我过去曾经多次面对这个问题,而且我从未找到过一个非常好的解决方案.请注意,作为一个小型的多平台项目,为缺乏经验的程序员构建简单性可以扩展它是必不可少的,我不想使用boost.但是,我们解决的平台的每个编译器都std::tr1绑定了.问题是:如何定义泛型函数的容器,每个容器接受可变数量的参数(最多N~5),并且是不是从公共虚拟类派生的对象的不同成员方法?谢谢

c++ templates scheduling callback generic-programming

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