复制构造函数vs移动构造函数在lambda捕获列表中

时间:2016-10-14 04:29:00

标签: c++ lambda c++14 capture

我对UXntu上OSX与GCC的clang行为感到困惑。

上下文:我正在尝试在OSX上构建一个库。不知道该库是否可以在OSX上构建,而这个问题与其原因无关;相反,我质疑我对lambda捕获列表中c ++移动构造函数语义的理解。

lambda表达式有一个使用std :: move的捕获。根据我对C ++的理解,应该选择一个移动构造函数来初始化捕获创建的隐式自动变量。

这是我在Ubuntu 14.4上编译代码时发生的事情

当我在OSX上编译相同的代码时,我收到以下错误[请参阅下面的代码段1 ],这表明选择了复制构造函数而不是移动构造函数。但是,代码会删除复制构造函数以使实例可移动,但不可复制[请参阅下面的代码段2 ]

Ubnutu上的GCC版

ubuntu@ip-10-0-0-174:~$ c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v 
  --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.3' 
  --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs 
  --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ 
  --prefix=/usr 
  --program-suffix=-4.8 
  --enable-shared 
  --enable-linker-build-id 
  --libexecdir=/usr/lib 
  --without-included-gettext 
  --enable-threads=posix 
  --with-gxx-include-dir=/usr/include/c++/4.8 
  --libdir=/usr/lib 
  --enable-nls 
  --with-sysroot=/ 
  --enable-clocale=gnu 
  --enable-libstdcxx-debug 
  --enable-libstdcxx-time=yes 
  --enable-gnu-unique-object 
  --disable-libmudflap 
  --enable-plugin 
  --with-system-zlib 
  --disable-browser-plugin 
  --enable-java-awt=gtk 
  --enable-gtk-cairo 
  --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre 
  --enable-java-home 
  --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 
  --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 
  --with-arch-directory=amd64 
  --with-ecj-jar=/usr/share/java/eclipse-ecj.jar 
  --enable-objc-gc 
  --enable-multiarch 
  --disable-werror 
  --with-arch-32=i686 
  --with-abi=m64 
  --with-multilib-list=m32,m64,mx32 
  --with-tune=generic 
  --enable-checking=release 
  --build=x86_64-linux-gnu 
  --host=x86_64-linux-gnu 
  --target=x86_64-linux-gnu
 Thread model: posix
 gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

OSX上的Clang版

shapira-MBA:wangle omer$ c++ -v
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin16.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

使用以下标志编译代码:

-I/Users/omer/git/wangle/wangle/..  
-I/usr/local/include 
-I/opt/local/include 
-I/Users/omer/git/wangle/wangle/gtest/src/gtest/googlemock/include 
-I/Users/omer/git/wangle/wangle/gtest/src/gtest/googletest/include  
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk   
-std=c++1y 
-fPIC

Snippet 1(带有额外的换行符,使编译器输出更具可读性,并删除无关的第二次捕获):

/Users/omer/git/wangle/wangle/../wangle/concurrent/FutureExecutor.h:63:11: note: 
   copy constructor of '' is implicitly deleted 
   because field '' has a deleted copy constructor
    [ promise = std::move(promise)]() mutable {
      ^
/usr/local/include/folly/futures/Promise.h:35:3: note: 'Promise' has been
   explicitly marked deleted here
   Promise(Promise const&) = delete;
   ^

Snippet 2(Promise类的构造函数):

// not copyable
Promise(Promise const&) = delete;
Promise& operator=(Promise const&) = delete;

// movable
Promise(Promise<T>&&) noexcept;
Promise& operator=(Promise<T>&&) noexcept;

我的问题是:根据哪个规则调用复制构造函数?这是UB的案例吗?

0 个答案:

没有答案