小编Woj*_*ilo的帖子

c ++指向运算符的指针

我想用c ++(或c ++ 0x)编写一个指针,指向一个类的运算符,让我们说A或B.有什么方法可以做到吗?

当然有一种语法

int (A::*_p) ();
Run Code Online (Sandbox Code Playgroud)

但它并没有解决这个问题.我想制作通用指针,而不是为它指定基类 - 只有"运算符函数"的指针

#include <thread>
#include <iostream>

using namespace std;

class A
{
public:
    int operator()()
    {
        return 10;
    }
};

class B
{
public:
    int operator()()
    {
        return 11;
    }
};

int main()
{
 A a;
 int (*_p) ();
 _p = a.operator();
 cout << _p();

 B b;
 _p = b.operator();
 cout << _p();
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers function-pointers operator-keyword c++11

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

JavaScript 2D渲染库[pixie.js vs three.js]

很少有JavaScript库允许使用WebGL渲染2D图形.我发现,这是最流行的是three.js所pixi.js.它们都允许您使用WebGL或canvas渲染器(对于不支持WebGL的设备).

我想问你在以下术语中哪些库更好:

  • 我希望它仅用于2D图形,因此3D支持是完全可选的.
  • 表现非常重要 - 很多元素,文字,平滑缩放,翻译等等都是非常重要的.
  • 画布渲染器(当设备不支持WebGl时很重要)我希望使用两个渲染器看到相同(或非常相似)的结果.

如果有另一个图书馆,我应该在这个特殊情况下调整,请随时告诉它:)

javascript performance html5 webgl three.js

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

C++和Haskell代码在不同的机器上的执行时间不同

我想问你是什么导致这种差异.如果我编译以下程序并运行相同的二进制文件 - 在某些平台上,由C++代码生成的程序比Haskell程序快得多,在其他情况下则相反.

另外,根据它们构建的平台,最终二进制文件的性能有很大差异.(每个平台使用相同的标志和相同版本的LVM和clang)

代码已经过优化,应该可以使用相同的性能 - 请参阅:Haskell能否以与Clang/GCC相同的方式优化函数调用?.

我想问你,怎么可能.

C++代码:

#include <cstdio>
#include <cstdlib>

int b(const int x){
    return x+5;
}

int c(const int x){
    return b(x)+1;
}

int d(const int x){
    return b(x)-1;
}

int a(const int x){
    return c(x) + d(x);
}

int main(int argc, char* argv[]){
    printf("Starting...\n");
    long int iternum = atol(argv[1]);
    long long int out = 0;
    for(long int i=1; i<=iternum;i++){
        out += a(iternum-i);
    }
    printf("%lld\n",out);
    printf("Done.\n");
}
Run Code Online (Sandbox Code Playgroud)

用.编译 clang++ -O3 main.cpp

haskell代码:

module Main …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction optimization performance haskell

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

Cabal有多个图书馆部分

是否可以编写Cabal包含多个配置文件的配置文件Library sections

我在文档中找到了Library 部分和Executables 部分的描述,看起来似乎不可能在一个Cabal配置文件中放入更多的Library部分.

但是,如果我
同时开发几个Haskell库和几个可执行文件并想要编译和测试它们,我该怎么办?

haskell build build-system cabal

11
推荐指数
3
解决办法
2313
查看次数

在向ManagedProcess发送消息时,Cloud Haskell永远挂起

问题

你好!我在Cloud Haskell中编写了一个简单的Server-Worker程序.问题是,当我尝试创建时ManagedProcess,在服务器发送步骤之后,我的示例即使在使用时也会永久挂起callTimeout(在100毫秒后应该会中断).代码很简单,但我发现它没有任何问题.

我也在邮件列表上发布了这个问题,但就我所知的SO社区而言,我在这里可以更快地得到答案.如果我从邮件列表中得到答案,我也会在这里发帖.

源代码

Worker.hs:

{-# LANGUAGE DeriveDataTypeable        #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE DeriveGeneric             #-}
{-# LANGUAGE TemplateHaskell           #-}

module Main where

import Network.Transport     (EndPointAddress(EndPointAddress))
import Control.Distributed.Process hiding (call)
import Control.Distributed.Process.Platform hiding (__remoteTable)
import Control.Distributed.Process.Platform.Async
import Control.Distributed.Process.Platform.ManagedProcess
import Control.Distributed.Process.Platform.Time
import Control.Distributed.Process.Platform.Timer (sleep)
import Control.Distributed.Process.Closure (mkClosure, remotable)
import Network.Transport.TCP (createTransport, defaultTCPParameters)
import Control.Distributed.Process.Node hiding (call)
import Control.Concurrent (threadDelay)
import GHC.Generics (Generic)
import Data.Binary (Binary) 
import Data.Typeable (Typeable)
import Data.ByteString.Char8 (pack)
import System.Environment …
Run Code Online (Sandbox Code Playgroud)

cloud haskell distributed-computing cloud-haskell

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

使用SBV和Haskell证明的符号理论

我在Haskell中使用SBV(带有Z3后端)来创建一些理论证明.我想检查forall xy给定的约束(比如x + y = y + x,哪里+是"加上运算符",而不是添加)其他一些术语是有效的.我想定义关于+表达式的公理(比如关联性,身份等),然后检查是否有进一步的等式,比如检查是否a + (b + c) == (a + c) + b有效正式a,bc.

我试图通过以下方式实现它:

main = do
    let x = forall "x"
    let y = forall "y"
    out <- prove $ (x .== x)
    print "end"
Run Code Online (Sandbox Code Playgroud)

但似乎我们不能.==在符号值上使用运算符.这是缺少的功能还是错误的用法?我们能够以某种方式使用SBV吗?

haskell z3 sbv

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

How to sandbox third party applications when `sandbox-exec` is deprecated now?

I've noticed that in the newest MacOS the sandbox-exec command is deprecated. According to it's manual:

The sandbox-exec command is DEPRECATED. Developers who wish to sandbox an app should instead adopt the App Sandbox feature described in the App Sandbox Design Guide. [...]

Moreover, a few commands were removed, like sandbox-simplify. Also, it seems that the trace function is not working anymore, the following config just does not produce output anymore (while it did in earlier versions):

(version 1) …
Run Code Online (Sandbox Code Playgroud)

security macos sandbox deprecated

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

用"纯"C++ 11替换替换BGL迭代顶点?

我想用纯C++ 11等效替换顶点或边上的BGL迭代.BGL代码(来自:http://www.boost.org/doc/libs/1_52_0/libs/graph/doc/quick_tour.html)是:

typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_end;
typename boost::graph_traits<Graph>::edge_descriptor e;
for (std::tie(out_i, out_end) = out_edges(v, g);
     out_i != out_end; ++out_i)
{
  e = *out_i;
  Vertex src = source(e, g), targ = target(e, g);
  std::cout << "(" << name[get(vertex_id, src)]
            << "," << name[get(vertex_id, targ)] << ") ";
}
Run Code Online (Sandbox Code Playgroud)

我从这里尝试了几个建议:用"纯"C++ 11替换替换BOOST_FOREACH?但没有运气.

我希望能够写出如下内容:

for (auto &e : out_edges(v, g))
{ ... }
Run Code Online (Sandbox Code Playgroud)

或类似的东西:

for (std::tie(auto out_i, auto out_end) = out_edges(v, g);
     out_i != out_end; ++out_i)
{...}
Run Code Online (Sandbox Code Playgroud)

可能吗?

c++ boost iterator boost-graph c++11

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

Haskell Parser组合器

我正在阅读很多关于Haskell Parser Combinators的内容,并发现了许多主题,如:

但是,所有这些话题比较Parser CombinatorsParser Generators.

我想问你哪个Parser Combinator最适合以下条件:

  1. 我希望能够很好地控制错误(包括错误恢复)和用户的消息
  2. 我希望能够用一小部分文本(不是一次整个文件)来提供解析器
  3. 我希望能够很好地重新设计语法(我目前正在开发语法,所以"很好的工作"非常重要"
  4. 最终的解析器应该很快(性能很重要,但不如第1-3点那么多).

我发现,最流行的解析器组合器是:

parsing haskell parsec parser-combinators attoparsec

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

如何在使用镜头访问不存在的字段时避免默认返回值?

我喜欢镜头库,我喜欢它的工作方式,但有时它引入了很多问题,我后悔自己开始使用它.让我们看看这个简单的例子:

{-# LANGUAGE TemplateHaskell #-}

import Control.Lens

data Data = A { _x :: String, _y :: String }
          | B { _x :: String }

makeLenses ''Data

main = do
    let b = B "x"    
    print $ view y b
Run Code Online (Sandbox Code Playgroud)

它输出:

""
Run Code Online (Sandbox Code Playgroud)

现在想象 - 我们有一个数据类型,我们通过改变一些名称来重构它.这个名称不再适用于特定的数据构造函数,而是获得错误(在运行时,与普通访问器一样),镜头使用memptyfrom Monoid来创建默认对象,因此我们得到奇怪的结果而不是错误.调试这样的东西几乎是不可能的.有没有办法解决这个问题?我知道有一些特殊的操作员可以获得我想要的行为,但是所有来自镜头的"正常"外观功能都非常糟糕.我应该用我的自定义模块覆盖它们还是有更好的方法?

作为旁注:我希望能够使用镜头语法读取和设置参数,但只删除字段缺失时自动结果创建的行为.

haskell haskell-lens

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