숙제를하면서 랩탑 컴퓨터의 컴파일러가 우리가 제출에 사용할 것으로 기대되는 컴퓨터의 컴파일러보다 훨씬 더 관대하다는 것을 알게되었습니다. 내 랩톱의 C++ 컴파일러는 AppleClang 7.0.2.7000181
이고 제출 상자의 컴파일러는 g++ 4.9.2
입니다. 생각해 보면 컴파일 안, 코드는 다음과 같습니다왜이 C++ 코드는 일부 컴파일러에서 컴파일되지만 다른 컴파일러에서는 컴파일되지 않습니까?
#include <iostream>
std::tuple<int, int> foo() {
return std::make_tuple(1, 1);
}
int main() {
auto pair = foo();
int x = std::get<0>(pair);
int y = std::get<1>(pair);
std::cout << x << "," << y << std::endl;
return 0;
}
나는 또한 가지고 CMakeLists.txt :
내 노트북에cmake_minimum_required(VERSION 2.8)
project(foo)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall -Werror -Werror=sign-compare")
set(SOURCE_FILES main.cpp)
add_executable(foo ${SOURCE_FILES})
는 연타 행복이 코드를 컴파일하고 1,1
을 출력합니다. 오류 없음, 경고 없음, 없음. 제출 상자에, 나는 그렇게 운이 좋다. 나는 tuple
헤더를 포함하고 있지 않다 때문에
/home/nate/foo/main.cpp: In function 'std::tuple<int, int> foo()':
/home/nate/foo/main.cpp:3:26: error: return type 'class std::tuple<int, int>' is incomplete
std::tuple<int, int> foo() {
^
/home/nate/foo/main.cpp:4:12: error: 'make_tuple' is not a member of 'std'
return std::make_tuple(1, 1);
^
/home/nate/foo/main.cpp: In function 'int main()':
/home/nate/foo/main.cpp:8:21: error: 'void pair' has incomplete type
auto pair = foo();
^
/home/nate/foo/main.cpp:9:13: error: 'get' is not a member of 'std'
int x = std::get<0>(pair);
^
/home/nate/foo/main.cpp:10:13: error: 'get' is not a member of 'std'
int y = std::get<1>(pair);
^
이러한 오류
은 이해하지만,이 코드는 내 노트북에 전혀 컴파일 이유를 이해하지 않습니다. 무슨 일 이니?
AppleClang의 헤더는 간접적으로 또는 std :: tuple 정의의 다른 소스를 포함 할 수 있습니다. 확실히 알기 위해서는 헤더를 봐야 할 것입니다. –
이식성이 보장되지 않는 코드의 문제점은 ... 음, 잘 모르겠습니까? –