如何在 Mac OSX 的 Xcode 中启用 C++17?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51288230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:41:50  来源:igfitidea点击:

How do I enable C++17 in Xcode for Mac OSX?

c++xcodec++17macos-high-sierraxcode9.4

提问by claytonjwong

How do I enable C++17 in Xcode (9.4.1) on OSX High Sierra (10.13.5)?

如何在 OSX High Sierra (10.13.5) 的 Xcode (9.4.1) 中启用 C++17?

回答by claytonjwong

Steps to use C++17 in Xcode (9.4.1) on OSX High Sierra (10.13.5):

在 OSX High Sierra (10.13.5) 上的 Xcode (9.4.1) 中使用 C++17 的步骤:

  1. Open existing or create a new C++ project in Xcode
  2. Click on the "show project navigator" button. It is located on the top-left section of Xcode window just below the minimize/maximize/close window buttons. It is the left-most icon and looks like a folder.
  3. Click on "Build Settings" and scroll down to find and expand the section "Apple LLVM 9.0 - Language - C++"
  4. Change the C++ Language Dialect combobox selection to "C++17 [-std=c++17]"
  1. 在 Xcode 中打开现有的或创建一个新的 C++ 项目
  2. 单击“显示项目导航器”按钮。它位于 Xcode 窗口的左上角,就在最小化/最大化/关闭窗口按钮的下方。它是最左边的图标,看起来像一个文件夹。
  3. 单击“Build Settings”并向下滚动以找到并展开“Apple LLVM 9.0 - Language - C++”部分
  4. 将 C++ Language Dialect 组合框选择更改为“C++17 [-std=c++17]”

Xcode Build Settings

Xcode 构建设置

Verification steps:

验证步骤:

Now when I output __cplusplus, I see 201703, and I am able to compile C++17 features, such as if constexpr.

现在当我输出 __cplusplus 时,我看到 201703,并且我能够编译 C++17 特性,例如 if constexpr。

template<class T>
int compute(T x) {
    if constexpr( supportsAPI(T{}) ) {
        // only gets compiled if the condition is true
        return x.Method();
    } else {
        return 0;
    }
}

int main(){
    cout << __cplusplus << endl;
    return 0;
}

Output:

输出:

201703
Program ended with exit code: 0