[英]Strange behavior of gcc. Are C++ object definitions with {} and = equal or not?
我正在测试将处理中完成的模拟转换为 C++ 的可能性。 但是奇怪的事情发生了:
class World {
public:
sarray<pAnt> dummy1 { new array<pAnt>(10) };//OK
sarray<pAnt> dummy2 = new array<pAnt>(10); //Compiler error?!?!?!*
smatrix<pAnt> dummyWorld1 { new matrix<pAnt>(100,100) };//OK
smatrix<pAnt> dummyWorld2 = new matrix<pAnt>(100,100);//OK
smatrix<int> ants = new matrix<int>(50,2);// OK
...
};
错误:
/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/klasaWorld.pde.cpp:27:
错误:无法将“Processing::sarrayProcessing::ptr<Ant >&”类型的非常量左值引用绑定到“Processing::sarrayProcessing::ptr<Ant >”类型的右值
sarray<pAnt> dummy2 = new array<pAnt>(10);//Compiler error???
“详细”编译器 output:
[1/2 3.5/秒] 构建 CXX object CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o 失败:CMakeFiles/MROWKI_0.1_once.dir/数据/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o /usr/bin/g++ -DDEF_MAXTHREADS=16 -DMULTITR -DVERSION_NUM=0.1 -I/data/wb/SCC/__working_copies/Processing2C/ PROJECTS/MROWKI/cppsrc -I/home/borkowsk/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/../../scripts/../lib/include -I/data/wb/SCC/__working_copies/symShell2/WBRTM_Linux -I/data/wb/SCC/__working_copies/symShell2/WBRTM_Linux/INCLUDE --verbose -std=c++11 -pthread -MD -MT CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/ PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o -MF CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.od -o CMakeFiles/MROWKI_0.1_once。目录/数据/wb/SCC/__working_copies/Processing2 C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o -c /data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp 使用内置规范。 COLLECT_GCC=/usr/bin/g++ OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 目标:x86_64-linux-gnu 配置:../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,Z56121CC37D7123E665C0F8,1cobj-F++-55C0F83 -prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable -libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32= i686 --with-a bi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable- checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) COLLECT_GCC_OPTIONS ='-D''DEF_MAXTHREADS=16''-D''MULTITR''-D''VERSION_NUM=0.1''-I''/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc''-我''/home/borkowsk/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/../../scripts/../lib/include''-I''/data/wb/SCC/__working_copies/symShell2/WBRTM_Linux ' '-I' '/data/wb/SCC/__working_copies/symShell2/WBRTM_Linux/INCLUDE' '-v' '-std=c++11' '-pthread' '-MD' '-MT' 'CMakeFiles/MROWKI_0 .1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o''-MF''CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/项目/米 ROWKI/cppsrc/project_at_once.cpp.od''-o''CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o''-c'' -shared-libgcc' '-mtune=generic' '-march=x86-64' /usr/lib/gcc/x86_64-linux-gnu/7/cc1plus -quiet -v -I /data/wb/SCC/__working_copies/ Processing2C/PROJECTS/MROWKI/cppsrc -I /home/borkowsk/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/../../scripts/../lib/include -I /data/wb/SCC/__working_copies/symShell2 /WBRTM_Linux -I /data/wb/SCC/__working_copies/symShell2/WBRTM_Linux/INCLUDE -imultiarch x86_64-linux-gnu -MD CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc /project_at_once.cpp.d -MF CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.od -MT CMakeFiles/MROWKI_0.1_once.dir/data/wb /SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o -D_GNU_SOURCE -D_REENTRANT -D DEF_MAXTHREADS=16 -D MU LTITR -D VERSION_NUM=0.1 /data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp -quiet -dumpbase project_at_once.cpp -mtune=generic -march=x86-64 -auxbase-strip CMakeFiles/MROWKI_0 .1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o -std=c++11 -version -fstack-protector-strong -Wformat -Wformat-security -o / tmp/ccaH1ylE.s
GNU C++11 (Ubuntu 7.5.0-3ubuntu1~18.04) 版本 7.5.0 (x86_64-linux-gnu) 由 GNU C 版本 7.5.0, MPFR 版本 1.4.0,0.1,MPC 版本 6.5.0 (x86_64-linux-gnu) 编译isl 版本 isl-0.19-GMP ^
这是从我的项目中提取的问题。 看起来一行是有罪的,但我不明白为什么:-)
#include <memory>
namespace Processing
{
template<class T>
class ptr
{
//std::shared_ptr<T> _ptr;
public:
ptr();
ptr(ptr<T>&);
//ptr(const ptr<T>&);//This removes the error, but can not be implemented properly!
};
template<class T>
class array
{
//T* content;
public:
~array();
array(size_t N);
};
template<class T>
class sarray
{ // When the following line is commented out the error disapper!
// so what is missing in ptr<T> ?
ptr< array<T> > _arr;//opaque smart ptr to array
//std::shared_ptr< array<T> > _arr;//std smart pointer is OK
public:
~sarray(){}
sarray(){}
sarray(array<T>* tab);
sarray<T>& operator = (array<T>* tab);
};
}
using namespace Processing;
void example()
{
sarray<bool> test1 = new array<bool>(2);// Compiler error?
sarray<bool> test2;
test2 = new array<bool>(2);//OK
sarray<bool> test3 { new array<bool>(2) };//OK
}
sarray<pAnt> dummy2 = new array<pAnt>(10);
是复制初始化语法。
语义上类似于sarray<pAnt> dummy2( sarray<pAnt>( new array<pAnt>(10) ) );
(直到 C++17)。
sarray
没有用户定义的复制构造函数,所以它有一个隐式的,它执行成员方式的复制。
问题是,作为成员之一的ptr
确实有一个用户定义的复制构造函数ptr(ptr<T>&)
。 从技术上讲,复制构造函数可以采用非常量引用。 在实践中它很少有用,因为非常量引用不会绑定到临时的.
因此错误 - cannot bind non-const lvalue reference of type '...ptr<Ant>&' to an rvalue of type 'ptr<Ant>'
解决方案可能很简单 - 将笨拙的复制构造函数更改为移动构造函数ptr(ptr<T>&&)
(或添加一个)。
附加说明:似乎sarray
有一个用户定义的析构函数。 根据3/5/0 的规则,它可能还需要一个用户定义的副本和/或移动构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.