Re: [問題] 一個有意思的問題 vector <const int>
※ 引述《spider391 (小乖)》之銘言:
: code 很短我就直接貼了
: ======================================================
: #include <iostream>
: #include <vector>
: using namespace std;
: int main()
: {
: vector<const int> tmp;
: tmp.push_back(99);
: tmp[0] = 10; // 很神奇這裡竟然可以允許更動
: cout << "tmp[0] is: " << tmp[0] << endl;
: }
: =======================================================
: 我讓vector內的元素為 const
: 但 temp[0] 他竟然允許賦值 !!
: 請問各位高手有什麼想法 <( ̄oo, ̄)/
: 補充一下:
: 這段 code 在 Visual Studio 2005 可以跑
: 我在 cygwin 上的 g++ (3.4.4) 上 compile 則會出現 compiler error!!
: 謝謝
在 C++ 標準裡對於 Container Library 的說明有包含這一段:
The type of objects stored in these components must meet
the requirements of CopyConstructible types (20.1.3),
and the additional requirements of Assignable types.
至少必須符合可用複製建構式和可指派(assign)兩個要求,
C/C++對於非標準的行為大多數是未定義的,
所以在不同編譯器上看運氣會有不同反應。
我發現 Wikipedia 也有把這一段寫進去
http://en.wikipedia.org/wiki/Vector_(C%2B%2B)#Design
The vector template can be instantiated with any type that
fulfills the CopyConstructible and Assignable requirements
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.168.104.246
→
12/20 03:06, , 1F
12/20 03:06, 1F
→
12/20 03:06, , 2F
12/20 03:06, 2F
→
12/20 11:20, , 3F
12/20 11:20, 3F
推
12/20 14:19, , 4F
12/20 14:19, 4F
→
12/20 14:20, , 5F
12/20 14:20, 5F
推
12/20 14:26, , 6F
12/20 14:26, 6F
→
12/20 14:27, , 7F
12/20 14:27, 7F
→
12/20 14:27, , 8F
12/20 14:27, 8F
→
12/20 14:35, , 9F
12/20 14:35, 9F
→
12/20 14:36, , 10F
12/20 14:36, 10F
→
12/20 14:37, , 11F
12/20 14:37, 11F
→
12/20 16:06, , 12F
12/20 16:06, 12F
→
12/20 16:08, , 13F
12/20 16:08, 13F
→
12/20 16:08, , 14F
12/20 16:08, 14F
→
12/20 16:09, , 15F
12/20 16:09, 15F
→
12/20 16:10, , 16F
12/20 16:10, 16F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):