我想以一次性方式调用函数调用.在Boost/C++中执行此操作的最佳方法是什么?
我想传递两个参数,不需要结果.
我的公司无意中从cvs切换到颠覆,现在我们都希望我们有cvs回来.我知道有一些工具可以将历史记录和变化从cvs迁移到svn,并且没有相应的工具可以反过来.有关如何执行此操作的任何建议或想法?
我首先需要提一下,我对Scheme很新,因此,下面的问题可能没有多大意义.
在学校,我们已经定义了代数数据类型,它们通常具有一个无效的构造函数和一些内部/外部的构造函数.
在这种特殊情况下,我感兴趣的是一个BTree二叉树类型(也许平衡,在未来的迭代),我想类似这样是哈斯克尔如何对待构造函数.我曾经看到过如何实现树方案,这里例如,但这不是我想要的.
我不想只是围绕列表做一个包装器.我只想写下这样的东西:
nil: -> BTree
node: BTree x T x BTree -> BTree
Run Code Online (Sandbox Code Playgroud)
然后让它知道我的意思:
flattenTree: BTree -> List
Run Code Online (Sandbox Code Playgroud)
然后,我将定义它被(假设left,right,key被定义):
(define flattenTree
(lambda (t)
(node (flattenTree (left t))
(key t)
(flattenTree (right t)))))
Run Code Online (Sandbox Code Playgroud)
另外,我欢迎有关正确缩进我的计划代码的建议......(并且请加以修改)
Guile看起来有点直接嵌入到C/C++项目中,但它在iOS或Android上的表现如何呢?它是否需要不适用于这些平台的第三方库?
与JavaScript或Lua相比,它如何作为可嵌入的脚本语言?
我试图将类型添加到一些数字球拍代码中,以期使其变得更快,但是我在下面的代码中一直在处理for / list宏扩展。
(: index-member ((Listof Any) (Listof Any) -> (Listof Index)))
(define (index-member xs ys)
(filter-not negative?
(for/list ([(ann i Index) (in-range (ann (length xs) Index))])
(if (member (list-ref xs i) ys) i -1))))
Run Code Online (Sandbox Code Playgroud)
此函数返回x的索引列表,每个x是y的成员。它可以在球拍中使用,但是我似乎无法通过Typed Racket的类型检查器。具体来说,错误是:
类型检查器:宏扩展中的错误-类型信息不足以进行类型检查。请在以下位置添加更多类型注释:(for / list(((ann i Index)(in-range(ann(length xs)Index)))))(if(if(member(list-ref xs i)ys)i -1) )
您是否可以提供注释,以克服类型检查器的问题和/或解释为什么这些类型注释不足?
我对Eli Bendersky给出的这个例子感到有些惊讶(http://eli.thegreenplace.net/2015/the-scope-of-index-variables-in-pythons-for-loops/)
>>> def foo():
... lst = []
... for i in range(4):
... lst.append(lambda: i)
... print([f() for f in lst])
...
>>> foo()
[3, 3, 3, 3]
Run Code Online (Sandbox Code Playgroud)
但是当我想到它时,它有一定道理 - lambda正在捕捉对i的引用,而不是我的价值.
所以解决这个问题的方法如下:
>>> def foo():
... lst = []
... for i in range(4):
... lst.append((lambda a: lambda: a)(i))
... print([f() for f in lst])
...
>>> foo()
[0, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud)
似乎这个工作的原因是,当我被提供给外部lambda时,外部lambda创建一个范围并取消引用i,将a设置为i.然后,返回的内部lambda保持对a的引用.
这是正确的解释吗?
采取以下简单程序:
#include <fstream>
int main()
{
std::ifstream in(".");
int x;
if (in)
in >> x;
}
Run Code Online (Sandbox Code Playgroud)
在Redhat 6,gcc 4.4.7上运行,没有错误
在Ubuntu 14.04 LTS,gcc 4.8.2上运行,没有错误
在Redhat 7,gcc 4.8.2上我得到:
Run Code Online (Sandbox Code Playgroud)terminate called after throwing an instance of 'std::ios_base::failure' what(): basic_filebuf::underflow error reading the file Aborted (cored dumped)
我认为这与以下内容有关:https : //gcc.gnu.org/bugzilla/show_bug.cgi?id=53984
但是,我不明白为什么它可以在Ubuntu上运行。
有想法吗?
您如何看待在C++中指定用户定义的运算符的想象力?
这样的用户定义的运算符可以通过运算符名称(允许的字符的任意序列?),它的优先级,关联性和arity(其他东西?)来定义.
它们可用于多种用途:帮助构建基于C++的"微小"DSL,用于列表理解等.
这个功能不会扩展语言的可能用途吗?什么是允许用户定义的运营商的其他语言?Lisp浮现在脑海中,还有什么?有关该主题的任何链接?
每次在Xcode中进行编译时,每次尝试等待condition_variable时都会收到错误消息。错误是“以类型为std :: __ 1 :: system_error的未捕获异常终止:条件变量等待失败:参数无效”
一切在Visual Studio 2013中都可以正常工作。如果我决定不等待多个线程的相同condition_variable,则代码可以正常工作。r
好的,代码。
main.cpp:
#include "ThreadPool.h"
int main(int argc, const char * argv[])
{
ThreadPool pool;
for (int i = 0; i < 10; ++i)
pool.sendWork();
std::this_thread::sleep_for(std::chrono::milliseconds(50000));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
线程池
#pragma once
#include <condition_variable>
#include <vector>
#include <thread>
class ThreadPool
{
protected:
std::condition_variable _condition;
private:
std::vector<std::thread> _threads;
void threadLoop();
public:
ThreadPool();
void sendWork();
};
Run Code Online (Sandbox Code Playgroud)
ThreadPool.cpp:
#include "ThreadPool.h"
ThreadPool::ThreadPool()
{
for (unsigned int i {0}; i < 10; ++i)
_threads.push_back(std::thread(&ThreadPool::threadLoop, this));
}
void …Run Code Online (Sandbox Code Playgroud) 这是我第一次尝试使用Racket的FFI.我想创建一个绑定到的应用程序,libgit2以便操作GIT存储库.
我需要做的第一件事是初始化一个存储库,如libgit2文档中所示:
git_repository *repo = NULL;
int error = git_repository_init(&repo, "/tmp/…", false);
Run Code Online (Sandbox Code Playgroud)
在Racket中获取函数调用很简单:
(require ffi/unsafe
ffi/unsafe/define)
(define-ffi-definer define-libgit (ffi-lib "/opt/local/lib/libgit2.dylib"))
(define _git_repository-ptr (_cpointer/null 'git_repository))
(define-libgit git_repository_init (_fun _git_repository-ptr _string _bool -> _int))
Run Code Online (Sandbox Code Playgroud)
但是,然后尝试使用该功能不起作用:
-> (define new_repo _git_repository-ptr)
-> (git_repository_init new_repo "/tmp/..." #f)
; git_repository->C: argument is not `git_repository' pointer
; argument: #<ctype>
; [,bt for context]
Run Code Online (Sandbox Code Playgroud)
libgit2没有提供初始化指针的功能,如Racket FFI文档示例所示.
这是定义可空指针并将其初始化为的正确方法NULL吗?
git_repository另一方面,是struct图书馆中定义的.我应该define-cstruct在Racket一侧使用它来正确使用它吗?这可能很麻烦,因为它struct是根据其他structs …