簡體   English   中英

clang 在 macOS 中編譯到哪個版本的 c++?

[英]clang compiling to which version of c++ in macOS?

    #include <iostream>
    #include <vector>
    using namespace std;

    int main()
    {
     if (__cplusplus == 201703L) std::cout << "C++17\n";
     else if (__cplusplus == 201402L) std::cout << "C++14\n";
     else if (__cplusplus == 201103L) std::cout << "C++11\n";
     else if (__cplusplus == 199711L) std::cout << "C++98\n";
     else std::cout << "pre-standard C++\n";

     vector<int> a;
     a.push_back(3);
     vector<int> b = move(a);
    }

據我所知,“移動語義”是 c++11 的一部分

輸出(使用 clang++ 命令編譯時沒有任何標志):C++98

那么移動語義是如何工作的呢?

我知道我可以 select 特定版本的 c++ 我想用 -std=c++xx 標志編譯,但這不是我想要的。

我想知道,在macOS中clang編譯到的c++的默認版本是什么?

我的 clang 正在編譯的默認版本也是 c++98,而 move() function 也存在於 c++98 中,但顯然它不執行移動語義(即創建 Rvalue Reference),而只是深度復制。

以下鏈接給出了找出編譯器正在編譯到哪個版本的 c++ 的答案:

https://stackoverflow.com/a/7132549/362589

這是c ++ 98中move()的答案的鏈接:

https://stackoverflow.com/a/65010079/13023201

clang 有一個選項可以指定您要使用的 C++ 版本: - std=xxx

就像 gcc 一樣,您可以要求 ISO 或 GNU 版本。 例如:

c++98
c++03
ISO C++ 1998 with amendments
gnu++98
gnu++03
ISO C++ 1998 with amendments and GNU extensions

似乎 clang 默認使用 GNU 風格:

默認的 C++ 語言標准是 gnu++14。

gcc 也是如此。

您的編譯器很可能默認使用 gnu98,這意味着您的 C++ 版本確實是 C++98 (== C++03),但擴展提供了附加功能,例如std::move

您可以 go 到編譯器資源管理器並測試以下代碼

#include <iostream>

int main() {
    std::cout << __cplusplus;
}

使用-std=gnu++98使用 x86-64 clang 11.0.0 編譯,程序輸出:

199711

如果使用了STRICT_ANSI,您可以檢查您的代碼是否使用 ISO 或 GNU 版本:

所有 c* 和 gnu* 模式之間的差異:

c* 模式定義“ STRICT_ANSI ”。

(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM