小编cod*_*nk1的帖子

将访客模式与模板派生类一起使用

我尝试使用模板化派生类实现访问者模式

我使用gcc 4.5

这里是VisitorTemplate.hpp,我专门在类Visitor中派生,但我希望能够处理任何类型:

编辑:感谢interjay的建议,代码现在编译并运行没有错误

#ifndef VISITORTEMPLATE_HPP_
#define VISITORTEMPLATE_HPP_

#include <iostream>
#include <string>
using namespace std;

template<class T> Derived;

class Visitor
{
  public:
    virtual void visit(Derived<string> *e) = 0;
};

class Base
{
  public:
    virtual void accept(class Visitor *v) = 0;
};

template<class T>
Derived: public Base
{
  public:
    virtual void accept(Visitor *v)
    {
       v->visit(this);
    }
    string display(T arg)
    {
       string s = "This is : " + to_string(arg);
       return s;
    }
};

class UpVisitor: public Visitor
{
   virtual void …
Run Code Online (Sandbox Code Playgroud)

c++ templates design-patterns visitor-pattern c++11

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

关于unique_ptr表演

我经常读到unique_ptr在大多数情况下比shared_ptr更受欢迎,因为unique_ptr是不可复制的并且具有移动语义; shared_ptr会因复制和引用计数而增加开销;

但是当我在某些情况下测试unique_ptr时,它看起来比它的对手明显更慢(在访问中)

例如,在gcc 4.5下:

编辑:打印方法实际上不打印任何内容

#include <iostream>
#include <string>
#include <memory>
#include <chrono>
#include <vector>

class Print{

public:
void print(){}

};

void test()
{
 typedef vector<shared_ptr<Print>> sh_vec;
 typedef vector<unique_ptr<Print>> u_vec;

 sh_vec shvec;
 u_vec  uvec;

 //can't use initializer_list with unique_ptr
 for (int var = 0; var < 100; ++var) {

    shared_ptr<Print> p(new Print());
    shvec.push_back(p);

    unique_ptr<Print> p1(new Print());
    uvec.push_back(move(p1));

  }

 //-------------test shared_ptr-------------------------
 auto time_sh_1 = std::chrono::system_clock::now();

 for (auto var = 0; var < 1000; ++var) 
 {
   for(auto it …
Run Code Online (Sandbox Code Playgroud)

c++ gcc unique-ptr c++11

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

执行功能列表

我想应用存储在列表中的函数

let functions = [(fun () -> print_string "fun 1"); (fun () -> print_string "fun 2")]
Run Code Online (Sandbox Code Playgroud)

使用像List.iter这样的高阶函数来显示"fun 1"和"fun 2"

有没有办法做到这一点 ?

ocaml function list

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

如何构建更快的Queue实现?

我注意到queue.ml由于其基于链表的实现(具有可变字段)而趋于相当慢

是否可以构建具有更快结构的队列,例如数组?

或者有没有办法实现更好的表现,但仍然是功能性的方式?

编辑:我想实现一个发布 - 订阅解决方案,成千上万的人在不到一毫秒的时间内排队并提供操作(对于图形交互式应用程序),所以我需要一些非常快的东西并且ocaml队列实现没有满足我的性能需求

queue performance ocaml functional-programming

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

模式匹配和构造函数

为什么我在编写这种模式匹配时会出错:

type t = A of int | B of float

let f = function
        | (A i | B f) -> true
        | _ -> false
Run Code Online (Sandbox Code Playgroud)

要么

let f = function
        | A i | B f -> true
        | _ -> false
Run Code Online (Sandbox Code Playgroud)

错误:变量f必须出现在此|的两侧 图案

let f = function
        | (A i | B i) -> true
        | _ -> false
Run Code Online (Sandbox Code Playgroud)

要么

let f = function
        | A i | B i -> true
        | _ -> false
Run Code Online (Sandbox Code Playgroud)

错误:此模式匹配float类型的int类型的值, …

ocaml functional-programming pattern-matching

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

使用其他类型的变量列表定义类型

我想编写一个模块(在Ocaml 3.12中),能够将Variant类型定义为现有不同类型的聚合

它可以是0到N种类型,因此是变量列表或集合

它看起来像这样:

type newtype = Type0 of type0 | Type1 of type1 | ... | TypeN of typeN
Run Code Online (Sandbox Code Playgroud)

当然,我想将其创作分解

首先,我尝试创建一个由仿函数参数化的模块"Composite":

module Composite ( T0 : sig type t end ) ( T1 : sig type t end ) = 
struct
  type t = T0.t | T1.t
end
Run Code Online (Sandbox Code Playgroud)

第一个难点:如何将变量的变量列表传递给'Composite'模块?

这是一个很好的方法吗?

edit1:Variant允许定义一个XOR类型定义(它是T0或T1,但不是两者); 如何定义OR类型定义(可以是T0或T1或两者)?

ocaml functional-programming module functor

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