<分区>
#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace std;
struct TestMe
{
TestMe() : i(10), j(20) {}
int i;
int j;
};
int main()
{
// Case I:
//vector<const int> vec;
/*
/usr/local/gcc-4.8.1/include/c++/4.8.1/ext/new_allocator.h:93:7: error: 'const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const int; __gnu_cxx::new_allocator<_Tp>::const_pointer = const int*; __gnu_cxx::new_allocator<_Tp>::const_reference = const int&]' cannot be overloaded
address(const_reference __x) const _GLIBCXX_NOEXCEPT
*/
// Case II:
//vector<const TestMe> vecTest;
// Case III:
//boost::shared_ptr<vector<const TestMe>> shVecTestMe;
//shVecTestMe = boost::make_shared<vector<const TestMe> >( );
return 0;
}
我已经在两个编译器中试过上面的代码:
1> http://www.compileonline.com/compile_cpp11_online.php
2>微软VS2010
第一个编译器无法接受所有情况(即 CaseI、Case II、Case III)。 但是,MS VS2010 接受所有这些。
问题1>这些案例有意义吗?也就是说,有没有必要用
vector<const int>
vector<const TestMe>
boost::shared_ptr<vector<const TestMe>>
防止后面修改包含的值。
问题 2> 为什么两个编译器对这些情况有不同的 react 。根据 C++ 标准,哪一个是正确的?
谢谢