1

在我的项目中,我使用Boost.Bimap来实现双向映射。

看看Godbolt 上这个非常简单的 MCVE,我在其中使用结构化绑定来打印正确映射的键值对(根据文档,它与std::map.

问题

它对于任何 g++ 版本 >= 7.4 及更高版本都可以正常编译,但是我需要使用 g++ 7.1。在这里,此代码失败并显示以下消息:

<source>: In function 'int main()':

<source>:11:20: error: 'std::tuple_size<const boost::bimaps::relation::structured_pair<boost::bimaps::tags::tagged<const long unsigned int, boost::bimaps::relation::member_at::right>, boost::bimaps::tags::tagged<const std::__cxx11::basic_string<char>, boost::bimaps::relation::member_at::left>, mpl_::na, boost::bimaps::relation::mirror_layout>>::value' is not an integral constant expression

   for (const auto& [key, value] : bm.right) {

我能够发现这是由于g++ 中的一个错误,该错误似乎已在更高版本中修复。

解决方法尝试(玩具示例,成功)

为了使结构化绑定与我的编译器版本一起工作,我尝试通过专门std::tuple_sizestd::tuple_elementstd::get. 有关更多信息,请参阅此 cppreference 链接

为简单起见,我首先用玩具结构成功地尝试了这个。以下是专业,请查看 godbolt.org 上的完整代码

struct SampleType {
  int a = 42;
  std::string b = "foo"s;
  double c = 3.141;
};

#if (__GNUC__ == 7) && (__GNUC_MINOR__ == 1)
  template <std::size_t N>
  decltype(auto) get(const ::SampleType& t) {
    if      constexpr (N==0) return t.a;
    else if constexpr (N==1) return t.b;
    else                     return t.c;
  }

  namespace std {
    // Tuple size is 3
    template <> struct tuple_size<::SampleType> : std::integral_constant<std::size_t, 3> {};

    // Define tuple types
    template <std::size_t N> struct tuple_element<N, ::SampleType> {
        // Deduce type from get() function template defined above
        using type = decltype(::get<N>(std::declval<::SampleType>()));
    };
  }
#endif

请注意,如果您删除#ifdeffor g++ 7.1.,编译将失败并出现与上述相同的错误 ( ...is not an integral constant expression)。(有趣:与boost::bimap仅在 g++ 7.4 及更高版本中编译良好的示例不同,玩具示例已经在 g++ 7.2 中成功)

解决方法尝试(原始示例,不成功)

现在,非常确信我找到了解决方案,我尝试做同样的事情,boost::bimap但我无助地失败了(在 godbolt.org 上查看):

template <std::size_t N>
decltype(auto) get(const bimap::right_map::value_type& bm) {
  if      constexpr (N==0) return bm.first;
  else if constexpr (N==1) return bm.second;
}

namespace std {
  // Tuple size is 2 -> key-value pair
  template <> struct tuple_size<bimap::right_map::value_type> : std::integral_constant<std::size_t, 2> {};

  // Define tuple types
  template <> struct tuple_element<0, bimap::right_map::value_type> { using type = std::string; };
  template <> struct tuple_element<1, bimap::right_map::value_type> { using type = std::size_t; };
}

错误消息太长,无法在此处发布(请参阅 Godbolt 输出),但基本上我知道get编译器不匹配“my”的重载。请注意,出于调试原因,我在我的代码中插入了以下行,以确保我实际上处理的是我的专业领域中的正确类型。

for (const auto& pair : bm.right) {
  // Make sure we capture the right type in the specializations above
  static_assert(std::is_same<
      decltype(pair),
      const bimap::right_map::value_type&
  >::value);
}

难道我做错了什么?还是这个错误对我的解决方法构成了不可逾越的障碍?

4

1 回答 1

1

我不认为这是你可以解决的问题。

这是一个较短的复制:

#include <tuple>

namespace N {
    struct X {
        template <typename T> void get() { }
    };
}

namespace std {
    template <> struct tuple_size<N::X> : integral_constant<size_t, 1> { };
    template <> struct tuple_element<0, N::X> { using type = int; };
}

namespace N {
    template <size_t I> decltype(auto) get(X const&) { return 42; }
}

int main() {
    auto [i] = N::X{};
}

这是一个有效的程序。[dcl.struct.bind]/4中的措辞说,强调我的:

在类成员访问查找 ([basic.lookup.classref])的范围内查找 unqualified-id get E,如果找到至少一个声明是函数模板,其第一个模板参数是非类型参数,初始值设定项是e.get<i>()。否则,初始值设定项是get<i>(e),其中 get 在关联的命名空间 ([basic.lookup.argdep]) 中查找。

N::X具有接受类型模板参数的成员函数模板的事实get()应该导致我们考虑 ADL 查找 on get,它应该找到非成员N::get。gcc 7.4 正确执行此操作,gcc 7.3 抱怨N::X::get()无法正常工作。


解决这个问题的唯一方法是以某种方式包装初始化程序。基本上做类似的事情:

auto [i] = wrap(N::X{});

Wherewrap返回一些绝对没有名为 的成员的新类型get,以便您可以提供所需的非成员。我不确定这里是否有不需要额外包装的解决方案。除了只使用 gcc 7.4 :-)

于 2019-05-07T17:11:03.947 回答