【问题标题】:Initialize a tuple of vectors in a class member initializer list在类成员初始化器列表中初始化向量元组
【发布时间】:2016-08-19 18:41:08
【问题描述】:

我有一个数据结构:

using Delaunay = CGAL::Delaunay_triangulation_3<K, Tds>;
using Cell_handle = Delaunay::Cell_handle;
using Vertex_handle = Delaunay::Vertex_handle;
using Edge_handle = std::tuple<Cell_handle, std::uintmax_t, std::uintmax_t>;
using geometry_tuple = std::tuple<std::vector<Cell_handle>,
                              std::vector<Cell_handle>,
                              std::vector<Cell_handle>,
                              std::vector<Edge_handle>,
                              std::uintmax_t,
                              std::vector<Vertex_handle>>;

我想在默认构造函数中初始化:

struct SimplicialManifold {
///  Default constructor with proper initialization
SimplicialManifold()
      : triangulation{std::make_unique<Delaunay>()},
        geometry{std::make_tuple(0, 0, 0, 0, 0, 0)} {}

std::unique_ptr<Delaunay> triangulation;
///< std::unique_ptr to the Delaunay triangulation

geometry_tuple geometry;
///< The geometric structure of the triangulation
};

上述代码在 Apple LLVM 版本 7.3.0 (clang-703.0.29) 中有效,但在 GCC 5.2 中因大量模板编译器而失败:

../src/S3Triangulation.h: In constructor ‘SimplicialManifold::SimplicialManifold()’../src/S3Triangulation.h:493:55: error: no matching function for call to
 ‘std::tuple<std::vector<CGAL::CCC_internal::CCC_iterator<CGAL::Concurrent_compact_container<CGAL::Triangulation_cell_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick, CGAL::Triangulation_ds_cell_base_3<CGAL::Triangulation_data_structure_3<CGAL::Triangulation_vertex_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_vertex_base_3<CGAL::Epick, CGAL::Triangulation_ds_vertex_base_3<void> > >, CGAL::Triangulation_cell_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick, CGAL::Triangulation_ds_cell_base_3<void> > >, CGAL::Parallel_tag> > > >, tbb::scalable_allocator<CGAL::Triangulation_cell_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick, CGAL::Triangulation_ds_cell_base_3<CGAL::Triangulation_data_structure_3<CGAL::Triangulation_vertex_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_vertex_base_3<CGAL::Epick, CGAL::Triangulation_ds_vertex_base_3<void> > >, CGAL::Triangulation_cell_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick, CGAL::Triangulation_ds_cell_base_3<void> > >, CGAL::Parallel_tag> > > > > >, false>, std::allocator<CGAL::CCC_internal::CCC_iterator<CGAL::Concurrent_compact_container<CGAL::Triangulation_cell_base_with_info_3<long unsigned int, CGAL::Epick, CGAL::Triangulation_cell_base_3<CGAL::Epick,

(有问题的代码是https://github.com/acgetchell/CDT-plusplus/blob/functional/src/S3Triangulation.h,从第 467 行开始。)

我查看了初始化列表,但它们不起作用:

geometry{std::initializer_list<std::vector<Cell_handle>,
                std::vector<Cell_handle>,
                std::vector<Cell_handle>,
                std::vector<Edge_handle>,
                std::uintmax_t,
                std::vector<Vertex_handle>>({0, 0, 0, 0, 0, 0})
        } {}

关于如何做到这一点并使 GCC 满意的建议?

【问题讨论】:

  • 您是否使用 -std=c++11 标志进行编译?
  • 是的,实际上是 -std=c++1y
  • tuple value-initializes 它的元素,所以这是多余的。
  • 哦,哈哈,这确实有效。我想在分配值之前访问 SimplicialManifold.geometry 是个坏主意。

标签: c++ c++11 gcc c++14


【解决方案1】:

最好的解决方案是编写一个适当的结构或类,而不是滥用std::tuple。例如:

struct geometry_tuple
{
    std::array<std::vector<Cell_handle>, 3> cells;
    std::vector<Edge_handle> edges;
    std::uintmax_t max;
    std::vector<Vertex_handle> vertexes;

    geometry_tuple()
        : max(0)
    {
        // ...
    }
};

通过为每个字段设置描述性名称,而不是稍后再使用 .get&lt;2&gt;() 等,这使您的代码更清晰。它让您可以使用您可能需要的任何参数创建适当的构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-28
    • 2015-02-07
    • 2019-09-10
    • 2015-09-25
    • 2011-07-14
    • 1970-01-01
    • 2017-03-25
    • 2015-02-28
    相关资源
    最近更新 更多