首页 > 解决方案 > gcc 的奇怪行为。带有 {} 和 = 的 C++ 对象定义是否相等?

问题描述

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

“详细”编译器输出:

dir/data/wb/SCC/__working_copies/Processing2C/PROJECTS/MROWKI/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 /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,fortran,objc,

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      
}

标签: c++gcccompiler-construction

解决方案



推荐阅读