2016-12-27 1 views
0
int main() { 
    struct WorkItem { 
    int node; 
    unsigned predecessorIndex = 0; 
    }; 

    auto x = WorkItem { 0 }; 

    return 0; 
} 

이 코드는 연타와 잘 컴파일 디폴트 값으로 필드와 구조체를 초기화하지만 GCC와 수 :은 왜 MSVC 및 GCC는

source_file.cpp: In function ‘int main()’:

source_file.cpp:9:25: error: no matching function for call to ‘main()::WorkItem::WorkItem()’ auto x = WorkItem { 0 }; ^

source_file.cpp:9:25: note: candidates are:

source_file.cpp:4:10: note: main()::WorkItem::WorkItem() struct WorkItem { ^

source_file.cpp:4:10: note: candidate expects 0 arguments, 1 provided

source_file.cpp:4:10: note: constexpr main()::WorkItem::WorkItem(const main()::WorkItem&)

source_file.cpp:4:10: note: no known conversion for argument 1 from ‘int’ to ‘const main()::WorkItem&’

source_file.cpp:4:10: note: constexpr main()::WorkItem::WorkItem(main()::WorkItem&&)

source_file.cpp:4:10: note: no known conversion for argument 1 from ‘int’ to ‘main()::WorkItem&&’

또는 MSVC :

source_file.cpp(9): error C2440: 'initializing': cannot convert from 'initializer list' to 'main::WorkItem'

source_file.cpp(9): note: No constructor could take the source type, or constructor overload resolution was ambiguous

Clang이 표준 관점에서이 코드를 잘못 컴파일했거나 MSVC와 GCC가 잘못 되었습니까? 또한, 왜 = 0을 제거하면 GCC와 MSVC를 컴파일 할 수 있습니까? 예 :

int main() { 
    struct WorkItem { 
    int node; 
    unsigned predecessorIndex = 0; 
    }; 

    auto x = WorkItem { 0 }; 

    return 0; 
} 

GCC 버전 : 4.9.3 연타 버전 : 3.7.0 MSVC 버전 : 19.00.24215.1

+0

모든 옵션을 포함하여 전체 컴파일 명령을 표시합니다. '-std ='옵션은 특히 결과에 영향을 미칠 것입니다. 또한 g ++의 아주 오래된 버전입니다. –

답변

4

GCC version: 4.9.3

기본 멤버 이니셜 라이저와

집계 초기화는 C++ 14 기능이있는 GCC does not support until GCC 5.x .

MSVC version: 19.00.23506

나는이 기본 멤버 이니셜 라이저와 2015 년 집계 초기화는 C++ 14 기능 VC doesn't support until 2017이다 VC의 업데이트 1 믿습니다.

+0

MSVC 버전이 잘못되었습니다. 19.00.24215.1이 맞습니다. –

+1

즉, C++ 11에서 'WorkItem'은 집계가 아니지만 C++ 14에서는 그렇습니다. –

+0

@HBellamy : 2015 년 업데이트 3입니다. 연도를 지정하고 번호를 업데이트하면 훨씬 쉬울 것입니다. 버전을 사용하면 의미를 찾아야합니다. VC가이 기능을 구현 한 것은 여전히 ​​VC 2017이 아닙니다. –