c++ - gcc的奇怪行为。带有{}和=的C++对象定义是否相等?

标签 c++ gcc compiler-construction

我正在测试将处理中完成的模拟转换为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:

    error: cannot bind non-const lvalue reference of type ‘Processing::sarrayProcessing::ptr<Ant >&’ to an rvalue of type ‘Processing::sarrayProcessing::ptr<Ant >’

     sarray<pAnt>    dummy2 = new array<pAnt>(10) ;//Compiler error?!?
    


  • “详细”编译器输出:

    [1/2 3.5/sec] Building CXX object CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o FAILED: CMakeFiles/MROWKI_0.1_once.dir/data/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.o.d -o CMakeFiles/MROWKI_0.1_once.dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o -c /data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/cppsrc/project_at_once.cpp Using built-in specs. COLLECT_GCC=/usr/bin/g++ OFFLOAD_TARGET_NAMES=nvptx-none OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../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,fortran,objc,obj-c++ --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-abi=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' '-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' '-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/PROJECTS/MROWKI/cppsrc/project_at_once.cpp.o.d' '-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.o.d -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 MULTITR -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,GMP版本6.1.2,MPFR版本4.0.1,MPC版本1.1.0,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没有用户定义的copy constructor,因此它得到一个隐式的,它执行成员级复制。
    问题是,作为成员之一的ptr确实具有用户定义的副本构造函数ptr(ptr<T>&)。从技术上讲,复制构造函数可以采用非const引用。实际上,它是rarely useful,因为非常量引用will not bind to a temporary
    因此错误-cannot bind non-const lvalue reference of type ‘...ptr<Ant>&’ to an rvalue of type ‘ptr<Ant>’一种解决方案可能很简单-将笨拙的副本构造函数更改为move-constructor ptr(ptr<T>&&)(或添加一个)。
    附加说明:sarray似乎具有用户定义的析构函数。根据rule of 3/5/0的描述,它可能也需要用户定义的副本和/或move-constructor。

    关于c++ - gcc的奇怪行为。带有{}和=的C++对象定义是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63314333/

    相关文章:

    c++ - 有没有办法声明一个类,然后在 C++ 的函数中初始化它?

    c - 为什么 sizeof 对 %f 的响应不同?

    C++警告如果在函数中重新声明成员变量

    c++ - 编译旧的 C++ unix 应用程序以在 windows 中使用

    c - 关于 C 编译器 asm 输出的另一个问题

    c++ - 在 C++ 中,严格的自底向上分析如何暗示返回类型不用于重载决策?

    c++ - C++中的动态分配数组

    C++测试与菜单应用

    c++ - 带有自定义键的 multimap - 比较功能

    c++ - gcc : inline of virtual function and especially destructor in my case 中内联的行为