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.





---------------------------------------my source 
code--------------------------------------
#include <functional>
#include <memory>
#include <string>


class Test {
public:
    Test( const char *in ): m_strMsg( in ) {}
    Test( Test && ) = default;
    Test( const Test & ) = delete;
    
private:
    std::string m_strMsg;
};


int main() {
    auto lambda_express = [ uptr = Test( "C++17" ) ]()->int { return 0; };
    std::function<int()> func1 = std::move( lambda_express );
    
    return 0;
}






------------------------------------error reported by gcc 
8.1----------------------------------------
----------Building project:[ training - Debug ]----------
make[1]: Entering directory '/home/mayuchao/workspaces/cpptraining/training'
codelite-cc /home/mayuchao/.opt/gcc/bin/g++  -c  
"/home/mayuchao/workspaces/cpptraining/training/main.cpp" -std=c++17 -g -O0 
-Wall  -o ./Debug/main.cpp.o -I. -I.
In file included from /home/mayuchao/.opt/gcc/include/c++/8.1.0/functional:59,
                 from /home/mayuchao/workspaces/cpptraining/training/main.cpp:1:
/home/mayuchao/.opt/gcc/include/c++/8.1.0/bits/std_function.h: In instantiation 
of 'static void 
std::_Function_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const 
std::_Any_data&, std::false_type) [with _Functor = main()::<lambda()>; 
std::false_type = std::integral_constant<bool, false>]':
/home/mayuchao/.opt/gcc/include/c++/8.1.0/bits/std_function.h:208:16:   
required from 'static bool 
std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const 
std::_Any_data&, std::_Manager_operation) [with _Functor = main()::<lambda()>]'
/home/mayuchao/.opt/gcc/include/c++/8.1.0/bits/std_function.h:676:19:   
required from 'std::function<_Res(_ArgTypes ...)>::function(_Functor) [with 
_Functor = main()::<lambda()>; <template-parameter-2-2> = void; 
<template-parameter-2-3> = void; _Res = int; _ArgTypes = {}]'
/home/mayuchao/workspaces/cpptraining/training/main.cpp:17:60:   required from 
here
/home/mayuchao/.opt/gcc/include/c++/8.1.0/bits/std_function.h:173:6: error: use 
of deleted function 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)'
      new _Functor(*__source._M_access<_Functor*>());
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/mayuchao/workspaces/cpptraining/training/main.cpp:16:52: note: 
'main()::<lambda()>::<lambda>(const main()::<lambda()>&)' is implicitly deleted 
because the default definition would be ill-formed:
     auto lambda_express = [ uptr = Test( "C++17" ) ]()->int { return 0; };
                                                    ^
/home/mayuchao/workspaces/cpptraining/training/main.cpp:16:52: error: use of 
deleted function 'Test::Test(const Test&)'
/home/mayuchao/workspaces/cpptraining/training/main.cpp:9:5: note: declared here
     Test( const Test & ) = delete;
     ^~~~
training.mk:95: recipe for target 'Debug/main.cpp.o' failed
make[1]: *** [Debug/main.cpp.o] Error 1
make[1]: Leaving directory '/home/mayuchao/workspaces/cpptraining/training'
Makefile:4: recipe for target 'All' failed
make: *** [All] Error 2
====3 errors, 5 warnings====



Reply via email to