C++标准:向量<T* 常数>

2022-01-23 00:00:00 pointers constants vector c++

我必须继续一个程序.我之前的程序员使用的结构很多:

std:vector

T* 常数>

他在 Visual Studio C++ 2010 中编写了 ist 并且能够编译它.我正在使用 g++,它会引发一些编译错误.

 g++ -g -Wall -c -std=c++11 -pedantic -I/usr/include/SuperLU/src/Cell.cpp -o obj/Cell.o在 src/Cell.cpp:13:0 包含的文件中:src/Cell.h:81:2:警告:额外的 ';'[-迂腐]在/usr/include/x86_64-linux-gnu/c++/4.7/./bits/c++allocator.h:34:0 包含的文件中,来自/usr/include/c++/4.7/bits/allocator.h:48,来自/usr/include/c++/4.7/string:43,来自/usr/include/c++/4.7/bits/locale_classes.h:42,来自/usr/include/c++/4.7/bits/ios_base.h:43,来自/usr/include/c++/4.7/ios:43,来自/usr/include/c++/4.7/ostream:40,来自/usr/include/c++/4.7/iostream:40,来自 src/Cell.cpp:2:/usr/include/c++/4.7/ext/new_allocator.h:在struct __gnu_cxx::new_allocator<BattPackage::Leg* const>"的实例化中:/usr/include/c++/4.7/bits/allocator.h:89:11: 'class std::allocator<BattPackage::Leg* const>' 需要/usr/include/c++/4.7/bits/alloc_traits.h:92:43: 来自 'struct std::allocator_traits<std::allocator<BattPackage::Leg* const>>'/usr/include/c++/4.7/ext/alloc_traits.h:110:10: 需要来自 'struct __gnu_cxx::__alloc_traits<std::allocator<BattPackage::Leg* const>>'/usr/include/c++/4.7/bits/stl_vector.h:76:28: 来自'struct std::_Vector_base<BattPackage::Leg* const, std::allocator<BattPackage::Leg* const>>'/usr/include/c++/4.7/bits/stl_vector.h:208:11: 'class std::vector<BattPackage::Leg* const>' 需要src/Cell.h:283:39:从这里需要/usr/include/c++/4.7/ext/new_allocator.h:83:7: 错误:'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with_Tp = BattPackage::Leg* 常量;__gnu_cxx::new_allocator<_Tp>::const_pointer = BattPackage::Leg* const*;__gnu_cxx::new_allocator<_Tp>::const_reference = BattPackage::Leg* const&]' 不能重载/usr/include/c++/4.7/ext/new_allocator.h:79:7: error: with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with_Tp = BattPackage::Leg* 常量;__gnu_cxx::new_allocator<_Tp>::pointer = BattPackage::Leg* const*;__gnu_cxx::new_allocator<_Tp>::reference = BattPackage::Leg* const&]

他想要这个结构是一个指针向量,他可以在其中添加和删除指针并操作指针的目标,但不能更改指针指向的对象.

据我了解,它不应该编译,因为 T* const 不可赋值.

有谁知道它为什么在 Visual Studio 中编译?我可以在声明中替换它而不需要更改完整的代码吗?

解决方案

std::vector 使用分配器为其成员分配、重新分配和解除分配内存.分配器要求仅针对类型 T 的分配器 X 定义,其中 T 是任何非常量、非引用对象类型"(C++11 表 27).所以使用 std::vector<T* const> 会给你带来未定义的行为,因为它使用的分配器不是为 const 元素类型定义的.

您最好尽快解决此问题.将声明更改为 std::vector<T*> 不太可能有很多问题.

i have to continue a programm. The programmer before me used the structure a lot:

std:vector< T* const>

He wrote ist in Visual Studio C++ 2010 and was able to compile this. I am using g++ and it thwrows some compilation errors.

    g++ -g -Wall -c -std=c++11 -pedantic -I/usr/include/SuperLU/ src/Cell.cpp -o obj/Cell.o
In file included from src/Cell.cpp:13:0:
src/Cell.h:81:2: warning: extra ';' [-pedantic]
In file included from /usr/include/x86_64-linux-gnu/c++/4.7/./bits/c++allocator.h:34:0,
                 from /usr/include/c++/4.7/bits/allocator.h:48,
                 from /usr/include/c++/4.7/string:43,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from src/Cell.cpp:2:
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of 'struct __gnu_cxx::new_allocator<BattPackage::Leg* const>':
/usr/include/c++/4.7/bits/allocator.h:89:11:   required from 'class std::allocator<BattPackage::Leg* const>'
/usr/include/c++/4.7/bits/alloc_traits.h:92:43:   required from 'struct std::allocator_traits<std::allocator<BattPackage::Leg* const> >'
/usr/include/c++/4.7/ext/alloc_traits.h:110:10:   required from 'struct __gnu_cxx::__alloc_traits<std::allocator<BattPackage::Leg* const> >'
/usr/include/c++/4.7/bits/stl_vector.h:76:28:   required from 'struct std::_Vector_base<BattPackage::Leg* const, std::allocator<BattPackage::Leg* const> >'
/usr/include/c++/4.7/bits/stl_vector.h:208:11:   required from 'class std::vector<BattPackage::Leg* const>'
src/Cell.h:283:39:   required from here
/usr/include/c++/4.7/ext/new_allocator.h:83:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = BattPackage::Leg* const; __gnu_cxx::new_allocator<_Tp>::const_pointer = BattPackage::Leg* const*; __gnu_cxx::new_allocator<_Tp>::const_reference = BattPackage::Leg* const&]' cannot be overloaded
/usr/include/c++/4.7/ext/new_allocator.h:79:7: error: with '_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = BattPackage::Leg* const; __gnu_cxx::new_allocator<_Tp>::pointer = BattPackage::Leg* const*; __gnu_cxx::new_allocator<_Tp>::reference = BattPackage::Leg* const&]

What he wanted with this structure was a vector of pointers, where he can add and remove the pointers and manipulate the target of the pointers but not change on which object the pointer points.

As far as I understand it should not compile because T* const is not assignable.

Does anyone know why it compiles in Visual Studio ? And can I replace that in the declarations without the need to change the complete code?

解决方案

std::vector uses an Allocator to allocate, reallocate, and deallocate memory for its members. The Allocator requirements are only defined for an Allocator X for type T, where T is "any non-const, non-reference object type" (C++11 Table 27). So using a std::vector<T* const> gives you undefined behaviour, because the allocator it uses is not defined for a const element type.

You're better off fixing this as soon as possible. There are unlikely to be many issues with changing the declarations to std::vector<T*>.

相关文章