About a error reported by gcc

Liu Hao lh_mouse@126.com
Thu Jul 5 02:25:00 GMT 2018


在 2018/7/5 9:14, snow_xmas 写道:
> Hello.
>      The source in the attachment can not be compiled, because there is a variable in the lambda-introducer does not have a copy-constructor, but have a move-constructor. When a function object constructed by a lambda-expression like this, the compiler will report this error. And I have try it under gcc version 5.4, 7,3 and 8.1, the same error will be reported by all these versions of gcc. However, the lambda-expression can be moved normally to std::async, std::packaged_task, except std::function. So I believe it's a bug of gcc
> 
>      The classes which is prohibited to copy but allowed to move is necessary in some condition, like unique_ptr. Please give me a solution for this error.
> 
> 
> 

The constructor of `std::function` in question requires the argument 
object to be CopyConstructible:

   ISO/IEC WG21 N1750
   Working Draft, Standard for Programming Language C++

   23.14.13.2.1 function construct/copy/destroy [func.wrap.func.con]

     template<class F> function(F f);

   7 Requires: F shall be CopyConstructible.

, which, recursively, requires everything in your lambda that is 
captured by value to be CopyConstructible.

As an alternative, using `uptr = std::make_shared<Test>("c++17")` in 
place of `uptr = Test("c++17")` will overcome this problem.

-- 
Best regards,
LH_Mouse



More information about the Gcc mailing list