小编olq*_*plo的帖子

为什么在std :: variant中禁止引用?

boost::variant经常使用它并且非常熟悉它.boost::variant不以任何方式限制有界类型,特别是它们可能是引用:

#include <boost/variant.hpp>
#include <cassert>
int main() {
  int x = 3;
  boost::variant<int&, char&> v(x); // v can hold references
  boost::get<int>(v) = 4; // manipulate x through v
  assert(x == 4);
}
Run Code Online (Sandbox Code Playgroud)

我有一个真正的用例,用于使用引用的变体作为一些其他数据的视图.

然后我惊讶地发现,这std::variant不允许引用作为有界类型,std::variant<int&, char&>不编译,它在这里明确地说:

不允许变量保存引用,数组或类型void.

我想知道为什么这是不允许的,我没有看到技术原因.我知道实现std::variantboost::variant不同,所以也许它与此有关?或者作者认为它不安全?

PS:我真的不能变通的限制std::variant使用std::reference_wrapper,这是因为参考包装不允许从基本类型分配.

#include <variant>
#include <cassert>
#include <functional>

int main() {
  using int_ref = std::reference_wrapper<int>;
  int x = 3;
  std::variant<int_ref> v(std::ref(x)); // v can hold references …
Run Code Online (Sandbox Code Playgroud)

c++ boost variant c++17

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

setuptools:从 C++ 代码构建共享库,然后构建链接到共享库的 Cython 包装器

我们有一堆 C++ 文件,其中包含我们使用 Cython 包装到 Python 的类。我们使用 setuptools 来构建 Cython 扩展。这一切都很好,我们遵循这里的指南: http ://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html

我们基本上是在做这样的事情

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize(
           "rect.pyx",                 # our Cython source
           sources=["Rectangle.cpp"],  # additional source file(s)
           language="c++",             # generate C++ code
      ))
Run Code Online (Sandbox Code Playgroud)

我们不喜欢这样,我们必须重新编译所有内容,即使rect.pyx在本例中只有 Cython 部分发生变化。事实上我们从来不碰.cpp文件,但.pyx经常更改文件。

我们希望将.cpp文件单独编译成静态或共享库,然后.pyx独立构建文件,该文件链接到从文件生成的库.cppmake使用或,所有这些都会很容易cmake,但我们想要一个仅使用setuptools. 模拟代码看起来像这样:

from distutils.core import setup
from Cython.Build import cythonize

class CppLibary:
    # somehow get that to work …
Run Code Online (Sandbox Code Playgroud)

c++ python build setuptools cython

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

标签 统计

c++ ×2

boost ×1

build ×1

c++17 ×1

cython ×1

python ×1

setuptools ×1

variant ×1