4

我对该功能有迫切的需求std::forward_as_tuple,但仅限于使用 GCC 4.5.0(我知道这是一个糟糕的情况,但它会为我解决很多问题,所以请保持尖刻的言论最低限度)。标<tuple>头似乎不包含该功能(应该如此),所以我的问题是:

  1. 它是否隐藏在其他标题中?(这种情况以前发生过,但很难确定。)
  2. 是否可以推出自己的实现?那就是:它是否可以用在 GCC 4.5.0中实现的 c++11 的部分来实现?如果有人真的知道如何做到这一点,那么奖金。
4

2 回答 2

5

实现很简单:

template <typename... Elements>
/*constexpr*/ tuple<Elements&&...>
forward_as_tuple(Elements&&... args) /* noexcept */
{
    return tuple<Elements&&...>(std::forward<Elements>(args)...);
}

不知道它出现在哪个 GCC 中。根据this document variadic templates和rvalue refs are available since gcc 4.3,所以它应该适用于你的gcc 4.5(我希望)

于 2012-12-18T03:52:33.727 回答
1

它是否隐藏在其他标题中?(这种情况以前发生过,但很难确定。)

有什么难的grep

标题<tuple>似乎不包含该功能(因为它应该)

std::forward_as_tuple最初被称为std::pack_arguments并于 2010 年 3 月在N3059中提出,并首次出现在 N3092 工作草案中。GCC 4.5.0 于 2010 年 4 月发布,当时该草稿上的墨水几乎未干。

Feel free to try and use C++11 features in an unmaintained, pre-C++11 compiler, but it's a bit unfair to say it should include features that didn't even exist when the release branch was cut and being prepared for a new release!

You should at least use GCC 4.5.4, using a dot-oh release is just asking for trouble, it will be full of new bugs that are fixed in later 4.5.x releases (although it still doesn't include forward_as_tuple or pack_arguments, they first appeared in GCC 4.6)

You could consider using boost::tuple instead, which attempts to provide a feature-complete implementation even for older compilers.

于 2012-12-18T10:25:31.580 回答