2012-10-16 38 views
2

我有以下代碼(儘可能多的簡化爲可能的):的std ::的for_each編譯克誤差++,不VS2012

#include <vector> 
#include <algorithm> 

using std::vector; 

enum class Foo { 
    BAR, BAZ 
}; 

void print_to_file(const vector<const vector<Foo> >& sequences) { 
    std::for_each(sequences.begin(), sequences.end(), [](const vector<Foo>& sequence) { 
    }); 
} 

int main(int argc, char **argv) { 
    return EXIT_SUCCESS; 
} 
g++ (SUSE Linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773]編譯爲 g++ --std=c++11 test.cpp

我得到以下錯誤消息:

In file included from /usr/include/c++/4.7/x86_64-suse-linux/bits/c++allocator.h:34:0, 
       from /usr/include/c++/4.7/bits/allocator.h:48, 
       from /usr/include/c++/4.7/vector:62, 
       from test.cpp:1: 
/usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘struct __gnu_cxx::new_allocator<const std::vector<Foo> >’: 
/usr/include/c++/4.7/bits/allocator.h:89:11: required from ‘class std::allocator<const std::vector<Foo> >’ 
/usr/include/c++/4.7/bits/alloc_traits.h:89:43: required from ‘struct std::allocator_traits<std::allocator<const std::vector<Foo> > >’ 
/usr/include/c++/4.7/ext/alloc_traits.h:109:10: required from ‘struct __gnu_cxx::__alloc_traits<std::allocator<const std::vector<Foo> > >’ 
/usr/include/c++/4.7/bits/stl_vector.h:76:28: required from ‘struct std::_Vector_base<const std::vector<Foo>, std::allocator<const std::vector<Foo> > >’ 
/usr/include/c++/4.7/bits/stl_vector.h:208:11: required from ‘class std::vector<const std::vector<Foo> >’ 
test.cpp:11:25: 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 = const std::vector<Foo>; __gnu_cxx::new_allocator<_Tp>::const_pointer = const std::vector<Foo>*; __gnu_cxx::new_allocator<_Tp>::const_reference = const std::vector<Foo>&]’ 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 = const std::vector<Foo>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::vector<Foo>*; __gnu_cxx::new_allocator<_Tp>::reference = const std::vector<Foo>&]’ 

相同的代碼與VS2012編譯得很好,但在g ++下失敗,我沒有絲毫的想法如何解釋錯誤信息。

+1

編譯下克++中的MinGW 4.7.0細。 – Yuushi

+0

@Yuushi我正在運行OpenSuse的標準安裝,如果除了'g ++ --version'字符串之外我還需要包含一些診斷信息,請告訴我。 – Voo

+0

嘗試使用g ++ -v來獲取它在構建時如何配置的清單 – Yuushi

回答

5

我很驚訝你不會在別處遇到問題,因爲這個vector<const vector<Foo> >不是可能的數據類型。

std::vector的value_type必須至少爲可複製構造(C++ 03)或可移動(C++ 11)。如果它是const,則不能分配或移動它。

+0

令人驚訝的是它適用於MinGW下的g ++。嗡嗡聲'const'對於它所具有的小效果來說真的是太多了......所以外帶的是我不能在矢量中保持常量。 – Voo

+0

這取決於你在做什麼。有些事情可能適用於某些編譯器,但這只是偶然。當插入或刪除對象時,實現可能需要在矢量內部移動東西,如果元素是常量,則不能這樣做。 –

+0

'const std :: vector '不可複製構造嗎?作業是一個明確的不,建設,但沒有問題。 – Yakk

3

用於標準容器的分配器要求不允許const對象被容器佔用。 C++ 11 17.6.3.5「分配器要求」概述了使用從表27開始的一組表格的標準分配器的要求。在表27中,第一個定義適用於'描述性變量'TUC,它們是定義爲「任何非常量,非參考對象類型」。描述性變量XY,用於分配器類defintions,有:

X - 類型T

Y一個分配器類 - 的相應分配器類型U

在總結,標準分配器是通過非常量類型的參數化定義的。

回到2004年,有一箇舊的GCC/libstdC++錯誤(解析爲無效)(C++ 2003對於在容器中存儲const對象有類似的限制,但它更容易顯示,因爲2003標準更直截了當地說存儲在容器中的對象的類型必須是「可分配的」)。

+0

是的,在bug報告中的一些評論基本上描述了我想要做的事情。擁有標準的不可變對象集合似乎不合理或很少有用。太糟糕了,似乎沒有辦法(至少直截了當)做到這一點。 – Voo