Search code examples
c++c++11tuplesgcc4perfect-forwarding

std::forward_as_tuple in G++ 4.5.0


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.

Solution

  • 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)