【问题标题】:Can't initialize std::tuple as std::pair?无法将 std::tuple 初始化为 std::pair?
【发布时间】:2014-09-17 23:09:05
【问题描述】:

根据我之前关于 std::pair 的 question,我认为我可以执行以下操作,但它不会编译。

typedef Point::FT float; 
std::vector<std::tuple<Point::FT, int, int> > branch{ (int)(max_leaf_check * mul_factor),
  { std::numeric_limits<Point::FT>::max(), -1 , -1} };

错误:

error: converting to ‘const value_type {aka const std::tuple<float, int, int>}’ from initializer list would use explicit constructor ‘std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) [with _UElements = {float, int, int}, <template-parameter-2-2> = void, _Elements = {float, int, int}]’

我该如何解决?如果无法修复,有没有其他方法可以在一行中进行初始化?

作为后续,我什至无法使用 push_back()

Point::FT new_dist;
size_t other_child_i, tree_i;
branch.push_back({new_dist, other_child_i, tree_i});

类似的错误。

【问题讨论】:

    标签: c++ c++11 stl tuples


    【解决方案1】:

    我该如何解决?

    你不能;显式构造函数防止隐式大括号初始化。

    如果无法修复,有没有其他方法可以在一行中进行初始化?

    std::make_tuple(std::numeric_limits<Point::FT>::max(), -1 , -1)
    

    作为后续,我什至无法使用push_back()

    branch.emplace_back(new_dist, other_child_i, tree_i);
    

    【讨论】:

    • branch 是一个向量,实际上是一个std::heap。我假设用emplace_back 替换push_back() 不会产生任何问题。正确的?另外,如果您能多说一点,为什么我的尝试无法修复?你的回答有效,我只是想了解和学习,如果可能的话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2018-01-13
    • 2020-03-26
    相关资源
    最近更新 更多