小编cod*_*nk1的帖子

如何将unique_ptr参数传递给构造函数或函数?

我是新手在C++ 11中移动语义,我不太清楚如何处理unique_ptr构造函数或函数中的参数.考虑这个引用自身的类:

#include <memory>

class Base
{
  public:

    typedef unique_ptr<Base> UPtr;

    Base(){}
    Base(Base::UPtr n):next(std::move(n)){}

    virtual ~Base(){}

    void setNext(Base::UPtr n)
    {
      next = std::move(n);
    }

  protected :

    Base::UPtr next;

};
Run Code Online (Sandbox Code Playgroud)

这是我应该如何编写unique_ptr参数的函数?

我需要std::move在调用代码中使用吗?

Base::UPtr b1;
Base::UPtr b2(new Base());

b1->setNext(b2); //should I write b1->setNext(std::move(b2)); instead?
Run Code Online (Sandbox Code Playgroud)

c++ arguments unique-ptr c++11

377
推荐指数
5
解决办法
15万
查看次数

如何在Ocaml中表示一个简单的有限状态机?

我用C++和Java编写了一些状态机,但从未使用像Ocaml这样的函数式语言

问题是我不知道我是否可以只调整对象语言版本的代码,因为在Ocaml中,记录和变体比类更强大;

所以,我需要一个事件驱动的有限状态机(像UML一样分层),可以轻松配置

在该领域有经验的人可以发布一个简单的样本吗?只是为了避免最常见的陷阱

谢谢 :)

编辑 16/03:没有可变状态可以做到吗?我想以"FSM"的名称正确封装它,我应该选择一个模块还是一个类?

state ocaml functional-programming fsm

13
推荐指数
4
解决办法
6895
查看次数

C++:两次调用虚函数之间执行时间的差异

gcc 4.5.1(Ubuntu 10.04,intel core2duo 3.0 Ghz)下考虑这个代码. 这只是2个测试,在第一个我直接调用虚拟fucnion,在第二个我通过Wrapper类调用它:

TEST.CPP

#define ITER 100000000

class Print{

public:

typedef Print* Ptr;

virtual void print(int p1, float p2, float p3, float p4){/*DOES NOTHING */}

};

class PrintWrapper
{

    public:

      typedef PrintWrapper* Ptr;

      PrintWrapper(Print::Ptr print, int p1, float p2, float p3, float p4) :
      m_print(print), _p1(p1),_p2(p2),_p3(p3),_p4(p4){}

      ~PrintWrapper(){}

      void execute()
      { 
        m_print->print(_p1,_p2,_p3,_p4); 
      }

    private:

      Print::Ptr m_print;
      int _p1;
      float _p2,_p3,_p4;

};

 Print::Ptr p = new Print();
 PrintWrapper::Ptr pw = new PrintWrapper(p, 1, 2.f,3.0f,4.0f);

void test1() …
Run Code Online (Sandbox Code Playgroud)

c++ performance virtual function c++11

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

我在哪里可以找到OCaml选件模块?

我的意思是这个模块:选项

我找不到它,open Option给我Error: Unbound module Option并且没有'option.cma'文件

它在标准库中吗?它被命名为'option.cma'吗?

ocaml functional-programming module

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

如何定义模块实现由仿函数参数化的模块签名

假设我有一个模块M参数化的模块F:

module M (F : sig type id type data end) =
struct
 type idtype = F.id
 type datatype = F.data
 type component = { id : idtype; data : datatype }
 let create id data = { id; data }
 let get_comp_data comp = comp.data
 let get_comp_id comp = comp.id
end
Run Code Online (Sandbox Code Playgroud)

所以我这样使用它:

module F1 = struct type id = int type data = float end
module MF1 = M(F1)

let comp = MF1.create 2 5.0 …
Run Code Online (Sandbox Code Playgroud)

ocaml functional-programming module functor

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

C++:根据参数的类型调用派生类的正确方法

假设我们有一个基类及其两个派生类; 基类拥有一个方法execute,每个派生类使用不同类型和数量的参数实现此方法的不同版本; 我不能使用虚方法,因为签名应该对每个派生类完全相同; 我的目标是提供一个基本执行方法,它接受任何类型的参数,减去它们的类型,并将它们分派到右派生类中的正确方法; 我看了一下Visitor模式,但我正在寻找一个更灵活,更优雅的解决方案;

编辑:我想将这些类存储在一个向量中,所以我需要一个基类

这是我的尝试(我不知道在基本执行的主体中放入什么)在gcc 4.5下:

class Base {

  public:

  Base();
  ~Base();

  template<typename ...Args>
  void execute(Args... arg)
  {
    //calls the right method
    //execute(int i) or execute(int i, float f)
    //as Args are int or int and float
  }

};

class DerivedA : public Base
{

  public:

  DerivedA();
  ~DerivedA();

  void execute(int i){ /*do something with i*/}

};

class DerivedB : public Base
{

  public:

  DerivedB();
  ~DerivedB();

  void execute(int i, float f){/*do something with i and f*/} …
Run Code Online (Sandbox Code Playgroud)

c++ virtual overriding c++11

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

如何从Ocaml中调用C++代码自己使用共享库.so?

我需要构建一个调用共享对象的Ocaml/C++模块(linux下的.so)

只要编译一个简单的Ocaml/C++存根是一个问题,我管理的东西,但是当我需要将.so与ocamlmklib或ocamlopt链接时,它会失败

我在gcc 4.5(c ++ 0x)下工作

共享对象的文件:

hello.hpp

#include <iostream>
#include <string>

using namespace std;

class HelloApplication
{
public :

    HelloApplication();
    ~HelloApplication();

    void say(string s);

};

typedef HelloApplication *(*create_hello)();
Run Code Online (Sandbox Code Playgroud)

hello.cpp:

#include "hello.hpp"

HelloApplication::HelloApplication(){}
HelloApplication::~HelloApplication(){}

void HelloApplication::say(string s)
{
     cout << "Hello : " << s << endl;
}

extern "C"
{
    HelloApplication *create()
    {
        return new HelloApplication();
    }

}
Run Code Online (Sandbox Code Playgroud)

编译事物的CMake.txt文件:

cmake_minimum_required(VERSION 2.6)

project(testHello_proj)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Release" FORCE)
#set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Debug" FORCE)

set(LIBRARY_OUTPUT_PATH lib/${CMAKE_BUILD_TYPE})

## Compiler flags …
Run Code Online (Sandbox Code Playgroud)

c++ linux interop ocaml shared-libraries

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

如何将函数应用于变体?

让这个类型=

type intC = int;;
type boolC = bool;
type stringC = string;;

type component = A of intC | B of boolC | C of stringC;;
Run Code Online (Sandbox Code Playgroud)

如果我想在组件A的类型a上应用函数,我是否需要系统地解构组件?

例如,我必须这样做:

let add comp =
  match comp with 
   | A i -> Some (i + 2) (*only A interests me, I return i + 2*)
   | _ -> None           (*otherwise I return nothing*)
Run Code Online (Sandbox Code Playgroud)

然后对组件A的任何功能?有什么意思可以避免你的冗余吗?

ocaml functional-programming

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

是否可以在构造函数中使用模板方法模式?

可能的重复:
在构造函数内调用虚函数

我有一个 Shape 类及其子类 Sphere :

//Shape : 

class Shape
{
    public:
        Shape(const string& name);
        virtual ~Shape();

        virtual string getName();

    protected:

        string mName;

};

Shape::Shape(const string& name) : mName(name)
{
   /*Some stuff proper to Shape*/

   /*Some stuff proper to subclass (sphere)*/

   /*Some stuff proper to Shape*/
}

Shape::~Shape(){}

string Shape::getName(){ return mName; }


//Sphere :

class Sphere : public Shape
{
    public:
        Sphere(const string& name, const float radius);
        virtual ~Sphere();

        virtual string getRadius();

    protected:

        float mRadius;
}

Sphere::Sphere(const string& …
Run Code Online (Sandbox Code Playgroud)

c++ virtual constructor template-method-pattern

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

如何处理多态变体列表?

让两种变体类型:

type typeA = 
    | A1 
    | A2
;;  

type typeB = 
    | B1 of typeA
    | B2 of typeA
;;  
Run Code Online (Sandbox Code Playgroud)

和类型检查功能:

let isA1 = function A1 -> true | _ -> false;;
let isA2 = function A2 -> true | _ -> false;;   
let isB1 = function B1 e -> true | _ -> false;;
let isB2 = function B2 e -> true | _ -> false;; 
Run Code Online (Sandbox Code Playgroud)

我想创建一个列表来检查A或B类型的元素

因为他们是不同类型,我需要多态变体,我得到:

type filterA =
{
    handleA : typeA -> bool;
};; …
Run Code Online (Sandbox Code Playgroud)

polymorphism ocaml functional-programming list variant

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