小编wal*_*mat的帖子

标头包含`protoc`生成的文件中的路径

当我打电话protoc这样的

protoc --cpp_out=. path/to/test.proto
Run Code Online (Sandbox Code Playgroud)

文件

  • path/to/test.pb.cc
  • path/to/test.pb.h

是我想要的。但是,由于cc需要h,所以h像这样包含

#include "path/to/test.pb.h"
Run Code Online (Sandbox Code Playgroud)

不是我想要的。背景是我的构建工具(sconsprotoc从项目的根目录而不是从包含源文件的目录中调用。我在手册页或帮助文本中找不到明显的选项。

因此,我的下一个想法是将其视为“正确”并调整构建系统,但是:这两个文件是目录树中的同级文件,因此当一个文件包含另一个文件时,则不需要路径。甚至手工编译也会失败。

有人可以帮我吗?

protocol-buffers

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

处理范围变量的内部函数

我有一段这样的代码:

std::list<boost::shared_ptr<Point> > left, right;
// ... fill lists ...

// now, calculate the angle between (right[0], right[1]) and (right[0], left[0])
double alpha = angle(*(right.begin()->get()), *(((++right.begin()))->get()), *(left.begin()->get()) );

std::cout << alpha * 180 / M_PI << std::endl;

if(alpha < 0){
    // do something with the lists, like reversing them. Especially the beginning and end of the lists may change in some way, but "left" and "right" are not reassigned.
}

// calculate the new alpha
alpha = angle(*(right.begin()->get()), *(((++right.begin()))->get()), *(left.begin()->get()) …
Run Code Online (Sandbox Code Playgroud)

c++ lambda

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

YAML 不调用构造函数

我尝试按照此处的说明进行操作,这使我找到了以下代码:

import yaml
class Step(yaml.YAMLObject):

    yaml_tag = "!step"

    def __init__(self, *args, **kwargs):
        raise Exception("Intentionally.")

yaml.load("""
--- !step
    foo: bar
    ham: 42
""")
Run Code Online (Sandbox Code Playgroud)

预期行为:我得到一个例外。但我观察到的是,我的 YAML 标记生成了一个Step实例,我可以使用它、访问方法、属性(如foo上面的代码)等等。阅读文档,我找不到我的错误,因为它表明使用所有键值对作为关键字参数调用构造函数。

基本上文档中的示例有效,但不是因为构造函数的实现,而是因为键值对( 的属性Monster)用于填充对象的字典。

这里有人知道吗?

我正在使用 python3,但在 python2 中进行了快速评估并观察到相同的情况。

编辑

我想做的事:为了保持在链接的例子(文档),如果Monster小号name带开始B,双倍的价值ac

python yaml python-3.x

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

优先级队列:使用对象进行比较而不是类

priority_queues 开始,我遇到了这样的问题:我需要将元素存储在队列中,但是它们如何排序的标准不包含在元素本身中,而是在某个地方不同,例如在地图中:

std::map<element, value> element_values;
std::priority_queue<element> queue;
Run Code Online (Sandbox Code Playgroud)

我现在需要的是这样的:

struct Comp
{
    std::map<...>& the_map;
    Cpmp(std::map<...> _map) : the_map(_map) {}

    bool operator() (element a, element b)
    {
        return the_map[a] < the_map[b];
    }
 }

 Comp comp(element_values);
 std::priority_queue<element, std::vector<element>, comp> queue; // does not work
 std::priority_queue<element, std::vector<element>, Comp> queue; // does work but I'd not be able to pass values to the constructor
Run Code Online (Sandbox Code Playgroud)

元素本身没有任何内在的顺序.一个解决方法是定义一个包含这个东西的结构,但也许有人知道一个更聪明的方法.我还考虑过提供一个只在我当前范围内有效的比较函数(它本身就是一个函数),但据我所知C++不支持,至少不能像我需要的那样捕获局部变量.

c++ priority-queue

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

在Python <3中有类似'nonlocal'的东西吗?

我得到了一段这样的代码:

foo = None

def outer():
    global foo
    foo = 0

    def make_id():
        global foo
        foo += 1
        return foo


    id1 = make_id() # id = 1
    id2 = make_id() # id = 2
    id3 = make_id() # ...
Run Code Online (Sandbox Code Playgroud)

我发现foo在最外面的scop中定义它很难看,我宁愿只在outer函数中使用它.据我所知,在Python3中,这是通过nonlocal.对于我想拥有的东西,有更好的方法吗?我更愿意申报和分配foo,outer并可能在delcare globalinner:

def outer():
    foo = 0

    def make_id():
        global foo
        foo += 1     # (A)
        return foo

    id1 = make_id() # id = 1
    id2 = make_id() …
Run Code Online (Sandbox Code Playgroud)

python python-nonlocal

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

模板中的嵌套类型名称

我想写一个这样的函数模板:

template< typename L<T> >  // does not work
void do_sth(L<T>& list){
    T value = 0;
    list.push_back(value);
}
Run Code Online (Sandbox Code Playgroud)

这意味着,在模板中我想使用提供"push_back"的容器,但也使用存储在该容器内的类型.

解决方法是

template< typename T >
void do_sth(typename std::list<T>& list){
    T value = 0;
    list.push_back(value);
}

// call    
std::list<double> list;
do_sth<double>(list);
Run Code Online (Sandbox Code Playgroud)

这是多余的,因为a)我在声明"list"时已经指定了"double",而b)该函数不适用于std :: vector,尽管实现完全适合.

有谁知道如何实现这一目标?

c++ templates

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