Bug 56774 - [4.7/4.8/4.9 Regression] G++ 4.8 reverses variadic template types during unpacking
[4.7/4.8/4.9 Regression] G++ 4.8 reverses variadic template types during unpa...
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: c++
4.8.0
: P3 major
: 4.7.3
Assigned To: Jason Merrill
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2013-03-28 21:22 UTC by Shane
Modified: 2013-03-30 00:31 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-03-28 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Shane 2013-03-28 21:22:06 UTC
In g++ 4.8, it seems as if variadic templates are reversed when being unpacked.  Consider the following minimal example:

#include <tuple>
#include <iostream>

template <class T, class ... OtherT>
void something( std::tuple<T, OtherT...> & tup )
{
  std::cout << std::get<1>(tup) << std::endl;
}

int main()
{
  std::tuple<int, char, bool> myTuple(3, 'a', true);

  // Compiles OK in GCC 4.6.3 but NOT 4.8
  something<int, char, bool>( myTuple );

  // Compiles OK in GCC 4.8 but NOT 4.6.3
  something<int, bool, char>( myTuple );

  return 0;
}

The output of this, if the appropriate line is commented out, will be: 'a'.  To get code that previously worked under 4.6.3, the template parameters that will be unpacked have to be reversed.

See relevant Stack Overflow post here: http://stackoverflow.com/questions/15692112/gcc-4-8-is-reversing-variadic-template-parameter-pack
Comment 1 Paolo Carlini 2013-03-28 23:57:30 UTC
We badly need a reduced testcase not using the whole <tuple> (not to mention <iostream> of course)
Comment 2 Daniel Frey 2013-03-29 00:09:10 UTC
Here's a reduced testcase without any includes:

template< typename... > struct tuple {};

template< typename T, typename... Ts >
void f( tuple< T, Ts... >& ) {}

int main()
{
   tuple< int, bool, char > t;
   f< int, char, bool >(t);
}

This compiles with GCC 4.7.2 and GCC 4.8.0, but it shouldn't.
GCC 4.6.3 and Clang 3.2 reject the code as expected.

Output from GCC 4.6.3:

Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:9:26: error: no matching function for call to 'f(tuple<int, bool, char>&)'
source.cpp:9:26: note: candidate is:
source.cpp:4:6: note: template<class T, class ... Ts> void f(tuple<T, Ts ...>&)
Comment 3 Paolo Carlini 2013-03-29 00:17:49 UTC
Thanks a lot Daniel. Let's CC Jason.
Comment 4 Shane 2013-03-29 00:21:32 UTC
(In reply to comment #1)
> We badly need a reduced testcase not using the whole <tuple> (not to mention
> <iostream> of course)

Here's a more reduced test case.

template <class ... Args>
struct mytype {};

template <class T, class ... Args>
void something( mytype<T, Args...> )
{ }

int main()
{
  // Compiles OK in GCC 4.6.3 but NOT 4.8
  something<int, char, bool>( mytype<int, char, bool>() );

  // Compiles OK in GCC 4.8 but NOT 4.6.3
  something<int, bool, char>( mytype<int, char, bool>() );

  return 0;
}

I tried to make simpler cases using purely variadic templates (e.g. no class T), but the issue only shows up when there is an individually named template parameter as well as a variadic pack.  To get the error to show up it also requires types that cannot be implicitly converted to each other.
Comment 5 Jason Merrill 2013-03-30 00:31:50 UTC
Fixed for 4.7.3/4.8.1/4.9.