Bug 44162 - [C++0x] cannot match function ref argument to dependent std::enable_if::type
Summary: [C++0x] cannot match function ref argument to dependent std::enable_if::type
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-16 21:32 UTC by David Krauss
Modified: 2010-05-17 09:35 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Krauss 2010-05-16 21:32:14 UTC
Given some function

void ff(); // any signature

This function is callable:

void rcv_f( typename std::enable_if< true, decltype(ff) >::type const &f ) {}

This function template does not produce a candidate:

template< class F >
void rcv_f( typename std::enable_if< true, F >::type const &f ) {}

This function template prints two identical lines:

template< class F >
void rcv_f( F const &f ) {
    std::cerr << typeid( typename std::enable_if< true, F >::type ).name() << std::endl;
    std::cerr << typeid( F ).name() << std::endl;
}

The problem does not occur with functors. The first example still works if trivially made into a template, so the dependent argument matters rather than the dependent context.
Comment 1 Paolo Carlini 2010-05-16 22:17:11 UTC
Please provide a short self-contained testcase, thanks.
Comment 2 David Krauss 2010-05-17 06:06:07 UTC
Sorry, that was completely wrong. I thought I'd isolated a testcase with the above code plus int main() { return rcv_f<decltype(f)>(f); }, but that actually does work.

It seems the problem is completely different. The testcase below suggests that decltype somehow cannot recurse in the presence of a reference to function type.

#include <tuple>
#include <type_traits> // aside: std::result_of belongs in <type_traits>
#include <functional> // not <functional>.

template< class T, class F,
    size_t tuple_index0 = std::tuple_size<T>::value,
    size_t... tuple_indexes >
typename std::result_of< typename std::enable_if< tuple_index0 == 0,
    F( typename std::tuple_element< tuple_indexes - 1, T >::type... )
>::type >::type
apply( T const &t, F f ) {
    return f( std::get< tuple_indexes - 1 >( t )... );
}

template< class T, class F,
    size_t tuple_index0 = std::tuple_size<T>::value,
    size_t... tuple_indexes >
decltype( apply< typename std::enable_if< tuple_index0 != 0, T >::type,
    F, tuple_index0 - 1, tuple_index0, tuple_indexes... >
    ( std::declval<T>(), std::declval<F>() ) )
apply( T const &t, F f ) {
    return apply< T, F, tuple_index0 - 1, tuple_index0, tuple_indexes... >
        ( t, f );
}

void f() {}
void g( int ) {}
void h( int, int ) {}

struct a { void operator()( int, int ) {} };

int main() {
    // these cases work:
    apply( std::make_tuple(), f );
    apply( std::make_tuple( 0 ), g );
    apply< std::tuple< int, int >, void (&)(int, int), 1, 2 >( std::make_tuple( 0, 0 ), h );
    apply( std::make_tuple( 0, 0 ), a() );

    // this does not:
    apply( std::make_tuple( 0, 0 ), h );
}

Either the subject of this bug should be changed or I should open a new bug.
Comment 3 Paolo Carlini 2010-05-17 09:35:04 UTC
Ok, then please open a new PR including a *minimal* testcase.