[cfe-dev] thread(F&&) fails when F is not MoveAssignable

Gordon Henriksen gordonhenriksen at me.com
Sun Dec 16 15:49:42 PST 2012


On 2012-12-16, at 15:36, Howard Hinnant wrote:

> On Dec 16, 2012, at 6:27 PM, Gordon Henriksen <gordonhenriksen at me.com> wrote:
> 
>> Thanks, Howard; that is an error in my reduction. The true test is this, and using 'bool *flag_;' instead of 'bool &flag_;' does make this case compile for reasons which are mysterious to me.
>> 
>> namespace {
>> 	class SimpleJoinThread {
>> 		bool &flag_;
>> 	public:
>> 		SimpleJoinThread(bool &flag) : flag_(flag) { }
>> 		void operator()() { flag_ = true; }
>> 	};
>> } // End anonymous namespace.
>> 
>> TEST(ThreadTests, SimpleJoin) {
>> 	bool flag = false;
>> 	std::thread(SimpleJoinThread(flag)).join();
>> 	EXPECT_TRUE(flag);
>> }
>> 
>> There's something more complex going on in the compilation unit, though; the reduced case I presented does compile on its own with the addition of void operator()() {}.
> 
> Is this a correct translation of the test?
> 
> #include <thread>
> #include <iostream>
> #include <cassert>
> 
> namespace {
>    class SimpleJoinThread {
>        bool &flag_;
>    public:
>        SimpleJoinThread(bool &flag) : flag_(flag) { }
>        void operator()() { flag_ = true; }
>    };
> } // End anonymous namespace.
> 
> int main()
> {
>    bool flag = false;
>    std::thread(SimpleJoinThread(flag)).join();
>    assert(flag);
> }
> 
> The above is compiling and running for me (without assert).  If it isn't for you, then perhaps we're looking at a libc++ or clang++ bug that has been recently fixed.

Yes, that translation does work for me.

> But first let's zero in on a precise copy/pasteable test.
> 
> I ran this test with both:
> 
> clang++ -std=c++11 -stdlib=libc++ test.cpp
> 
> and
> 
> clang++ -std=c++03 -stdlib=libc++ test.cpp

Agreed. I'm attempting to reduce a test case. The error log for the in situ failure is below.

— Gordon


In file included from .../ThreadTests.cpp:3:
In file included from ../../include/carbonite/compat/thread:17:
In file included from ../../include/carbonite/compat/emu/thread.h:8:
In file included from ../../include/carbonite/compat/chrono:8:
In file included from ../../include/carbonite/compat/emu/chrono.h:8:
In file included from ../../include/carbonite/compat/emu/chronofwd.h:8:
In file included from ../../include/carbonite/compat/ratio:8:
In file included from ../../include/carbonite/compat/emu/ratio.h:9:
In file included from ../../include/carbonite/compat/type_traits:15:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:3055:22: error: attempt to use a deleted function
    typedef decltype(swap(_VSTD::declval<_Tp&>(), _VSTD::declval<_Tp&>())) type;
                     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:3063:48: note: in instantiation of template class 'std::__1::__detail::__swappable<<anonymous>::SimpleJoinThread>' requested here
    : public integral_constant<bool, __detail::__swappable<_Tp>::value>
                                               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:3084:41: note: in instantiation of template class 'std::__1::__is_swappable<<anonymous>::SimpleJoinThread>' requested here
    : public __is_nothrow_swappable_imp<__is_swappable<_Tp>::value, _Tp>
                                        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:217:16: note: in instantiation of template class 'std::__1::__is_nothrow_swappable<<anonymous>::SimpleJoinThread>' requested here
    _NOEXCEPT_(__is_nothrow_swappable<_Hp>::value)
               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__config:253:34: note: expanded from macro '_NOEXCEPT_'
#  define _NOEXCEPT_(x) noexcept(x)
                                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:216:6: note: in instantiation of function template specialization 'std::__1::swap<0, <anonymous>::SimpleJoinThread, false>' requested here
void swap(__tuple_leaf<_Ip, _Hp, _Ep>& __x, __tuple_leaf<_Ip, _Hp, _Ep>& __y)
     ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:3063:48: note: in instantiation of template class 'std::__1::__detail::__swappable<std::__1::__tuple_leaf<0, <anonymous>::SimpleJoinThread, false>>' requested here
    : public integral_constant<bool, __detail::__swappable<_Tp>::value>
                                               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:3084:41: note: (skipping 1 context in backtrace; use -ftemplate-backtrace-limit=0 to see all)
    : public __is_nothrow_swappable_imp<__is_swappable<_Tp>::value, _Tp>
                                        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:332:44: note: in instantiation of template class 'std::__1::__is_nothrow_swappable<std::__1::__tuple_leaf<0, <anonymous>::SimpleJoinThread, false>>' requested here
    int swap(__tuple_leaf& __t) _NOEXCEPT_(__is_nothrow_swappable<__tuple_leaf>::value)
                                           ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__config:253:34: note: expanded from macro '_NOEXCEPT_'
#  define _NOEXCEPT_(x) noexcept(x)
                                 ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:436:14: note: in instantiation of template class 'std::__1::__tuple_leaf<0, <anonymous>::SimpleJoinThread, false>' requested here
    : public __tuple_leaf<_Indx, _Tp>...
             ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/tuple:523:10: note: in instantiation of template class 'std::__1::__tuple_impl<std::__1::__tuple_indices<0>, <anonymous>::SimpleJoinThread>' requested here
    base base_;
         ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/thread:350:36: note: in instantiation of template class 'std::__1::tuple<<anonymous>::SimpleJoinThread>' requested here
    _VSTD::unique_ptr<_Gp> __p(new _Gp(__decay_copy(_VSTD::forward<_Fp>(__f)),
                                   ^
.../ThreadTests.cpp:131:2: note: in instantiation of function template specialization 'std::__1::thread::thread<<anonymous>::SimpleJoinThread, , void>' requested here
        std::thread(SimpleJoinThread(flag)).join();
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/type_traits:825:5: note: function has been explicitly marked deleted here
    ~__nat() = delete;
    ^





More information about the cfe-dev mailing list