User account creation filtered due to spam.

Bug 65760

Summary: invalid use of incomplete type with std::is_convertible<C, C>
Product: gcc Reporter: Etienne Laurin <etienne>
Component: libstdc++Assignee: Jonathan Wakely <redi>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler
Priority: P3    
Version: 4.9.2   
Target Milestone: 6.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2015-04-15 00:00:00

Description Etienne Laurin 2015-04-14 05:08:58 UTC
The following code fails to compile with GCC 4.9.2 on Linux and OS X:

g++-4.9 -std=c++11 test.cc

I was not able to reduce the code any further:

* Commenting out 4, 5, 6, 7 or 11 causes the code to compile.

* Changing 'C c = C()' to 'C c' or 'C c; c = C()' also causes the code to compile.

Code:

#include <functional>

struct C {
    C() = default;

    C(std::function<C(int)>);
    C(std::function<C(int, int)>);
    template <class T> C operator () (T&&);
    template <class T> C operator () (T&&, T&&);
};

int main() {
    C c = C();
}

Error output:

In file included from /opt/gcc-4.9.2/include/c++/4.9.2/bits/move.h:57:0,
                 from /opt/gcc-4.9.2/include/c++/4.9.2/bits/stl_pair.h:59,
                 from /opt/gcc-4.9.2/include/c++/4.9.2/utility:70,
                 from /opt/gcc-4.9.2/include/c++/4.9.2/tuple:38,
                 from /opt/gcc-4.9.2/include/c++/4.9.2/functional:55,
                 from test-gcc.cc:1:
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits: In instantiation of ‘struct std::__and_<std::__not_<std::is_same<C, std::function<C(int, int)> > >, std::__or_<std::is_void<C>, std::is_convertible<C, C> > >’:
/opt/gcc-4.9.2/include/c++/4.9.2/functional:2163:63:   required by substitution of ‘template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond:: value, _Tp>::type [with _Cond = std::__and_<std::__not_<std::is_same<C, std::function<C(int, int)> > >, std::__or_<std::is_void<C>, std::is_convertible<C, C> > >; _Tp = void; _Res = C; _ArgTypes = {int, int}]’
/opt/gcc-4.9.2/include/c++/4.9.2/functional:2225:9:   required by substitution of ‘template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = C; <template-parameter-1-2> = <missing>]’
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:1401:39:   required by substitution of ‘template<class _From1, class _To1, class> static std::true_type std::__is_convertible_helper<_From, _To, false>::__test(int) [with _From1 = C; _To1 = C; <template-parameter-1-3> = <missing>]’
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:1410:30:   required from ‘struct std::__is_convertible_helper<C, C, false>’
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:1416:12:   required from ‘struct std::is_convertible<C, C>’
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:111:12:   required from ‘struct std::__or_<std::is_void<C>, std::is_convertible<C, C> >’
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:134:12:   required from ‘struct std::__and_<std::__not_<std::is_same<C, std::function<C(int)> > >, std::__or_<std::is_void<C>, std::is_convertible<C, C> > >’
/opt/gcc-4.9.2/include/c++/4.9.2/functional:2163:63:   required by substitution of ‘template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond:: value, _Tp>::type [with _Cond = std::__and_<std::__not_<std::is_same<C, std::function<C(int)> > >, std::__or_<std::is_void<C>, std::is_convertible<C, C> > >; _Tp = void; _Res = C; _ArgTypes = {int}]’
/opt/gcc-4.9.2/include/c++/4.9.2/functional:2225:9:   required by substitution of ‘template<class _Functor, class> std::function<_Res(_ArgTypes ...)>::function(_Functor) [with _Functor = C; <template-parameter-1-2> = <missing>]’
test-gcc.cc:14:13:   required from here
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:134:12: error: invalid use of incomplete type ‘std::conditional<true, std::__or_<std::is_void<C>, std::is_convertible<C, C> >, std::__not_<std::is_same<C, std::function<C(int, int)> > > >::type {aka struct std::__or_<std::is_void<C>, std::is_convertible<C, C> >}’
     struct __and_<_B1, _B2>
            ^
/opt/gcc-4.9.2/include/c++/4.9.2/type_traits:111:12: error: declaration of ‘std::conditional<true, std::__or_<std::is_void<C>, std::is_convertible<C, C> >, std::__not_<std::is_same<C, std::function<C(int, int)> > > >::type {aka struct std::__or_<std::is_void<C>, std::is_convertible<C, C> >}’
     struct __or_<_B1, _B2>
Comment 1 Jonathan Wakely 2015-04-15 11:57:00 UTC
The problem is that 'C c = C();' has to check for an accessible copy constructor, which needs to perform overload resolution on the constructors of C.

That means checking if the std::function<C(...)> arguments can be initialized from an rvalue of type C. std::function has SFINAE constraints to check if it is being constructed from a suitable type, and those constrraints require a complete type.

This fixes it by short-circuiting the constraints, but this might have unwanted consequences:

--- include/c++/5.0.0/functional.orig   2015-04-15 12:54:33.198940049 +0100
+++ include/c++/5.0.0/functional        2015-04-15 12:54:35.671952823 +0100
@@ -1962,7 +1962,7 @@
 
   template<typename _From, typename _To>
     using __check_func_return_type
-      = __or_<is_void<_To>, is_convertible<_From, _To>>;
+      = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>;
 
   /**
    *  @brief Primary class template for std::function.
Comment 2 Jonathan Wakely 2015-04-29 16:08:19 UTC
Author: redi
Date: Wed Apr 29 16:07:47 2015
New Revision: 222581

URL: https://gcc.gnu.org/viewcvs?rev=222581&root=gcc&view=rev
Log:
	PR libstdc++/65760
	* include/std/functional (__check_func_return_type): Use is_same to
	avoid using _is_convertible on incomplete types.
	* testsuite/20_util/function/65760.cc: New.

Added:
    trunk/libstdc++-v3/testsuite/20_util/function/65760.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/std/functional
Comment 3 Jonathan Wakely 2015-04-29 16:09:01 UTC
Fixed on trunk.