4

I have a pressing need for the function std::forward_as_tuple, but am restricted to using GCC 4.5.0 (I know this is a bad situation to put oneself into, but it would solve a lot of problems for me, so please keep the snarky remarks to a minimum). The <tuple> header does not seem to contain the function (as it should), so my question is:

  1. Is it hidden in some other header? (This has happened before, but is hard to determine.)
  2. Is it possible to roll your own implementation? That is: is it implementable with the parts of c++11 that is implemented in GCC 4.5.0? Bonus if anyone actually knows how to do this.
masaers
  • 697
  • 9
  • 21

2 Answers2

5

Implementation is simple:

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

Don't know in which GCC it appears. According this document variadic templates and rvalue refs are available since gcc 4.3, so it should work for your gcc 4.5 (I hope)

zaufi
  • 6,811
  • 26
  • 34
1

Is it hidden in some other header? (This has happened before, but is hard to determine.)

What's hard about grep?

The <tuple> header does not seem to contain the function (as it should)

std::forward_as_tuple was originally called std::pack_arguments and was proposed in N3059 in March 2010 and first appeared in the N3092 working draft. GCC 4.5.0 was released in April 2010 when the ink was barely dry on that draft.

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.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • I am deeply sorry if I have offended you (as your tone of voice suggests). It was not my intention. As I said, I am aware that the premise of my question is suboptimal, and that I am patching up things that are not complete. The GCC version is not under my control (if it was, I would use the latest), again, the question was put forth with the specific version as a premise. Thank you for suggesting Boost (which is not an option to me either, but I have now learned to be specific about that in my questions). – masaers Dec 27 '12 at 08:22