2012-10-16 3 views
2

내가 다음 코드 (가급적 간체)하지, ++ g와 오류를 컴파일 :표준 :의 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++ --std=c++11 test.cppg++ (SUSE Linux) 4.7.1 20120723 [gcc-4_7-branch revision 189773] 컴파일 나는 다음과 같은 오류 메시지가 얻을 :

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

는 4.7.0에서 ++는 MinGW g 미만 미세 컴파일. – 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 ++에 대해 놀랍습니다. 험 (hum)'const'는 그 효과가 너무 작아서 너무 많은 일을합니다. 그래서 테이크 어웨이는 단지 벡터로 일정한 것을 가질 수 없다는 것입니다. – Voo

+0

그것은 당신이하는 일에 달려 있습니다. 일부 컴파일러에서는 작동하지만, 우연히 발생합니다. 구현은 객체를 삽입하거나 삭제할 때 벡터 내부를 이동해야 할 수 있으며 요소가 const 인 경우에는 수행 할 수 없습니다. –

+0

'const std :: vector '이 어떻게 복사 가능하지 않습니까? 배정은 분명히 아니오, 건설은 문제가되지 않습니다. – Yakk

3

표준 컨테이너에 사용되는 할당 자 요구 사항은 const 개체가 컨테이너에 보관되는 것을 허용하지 않습니다. C++ 11 17.6.3.5 "할당 자 요구 사항"은 표 27로 시작하는 표 집합을 사용하는 표준 할당 자에 대한 요구 사항을 설명합니다. 표 27에서 첫 번째 정의는 '설명 변수'T, UC입니다. "비 const, 참조 오브젝트 유형이 아님"으로 정의됩니다. 할당 자 클래스되어 정의에 사용되는 설명 변수 XY는 같습니다

X - 형 T

Y 대한 할당 자 클래스 - 유형 U

에 해당하는 할당 자 클래스 요약, 표준 할당자는 비 const 유형에 의한 매개 변수화의 관점에서 정의됩니다.

2004 년으로 돌아가는 오래된 GCC/libstdC++ 버그 (무효로 해결됨)가 있습니다 (C++ 2003은 const 개체를 컨테이너에 저장하는 것과 비슷한 제약이 있지만 2003 표준은 더 간단하게 표시하기 때문에 표시가 간단합니다). 컨테이너에 저장된 오브젝트 유형이 '지정 가능'이어야 함).

+0

넵 버그 리포트의 몇 가지 코멘트는 기본적으로 내가하고 싶은 것을 설명합니다. 불변 객체의 표준 컬렉션을 갖는 것이 불합리하거나 드물게 유용하지는 않습니다.너무 나쁜 것 (적어도 직설적 인 방법)이 그렇게 보이지 않는 것 같습니다. – Voo