C++ 語言特性支援 (C++ Language Features)

重點總覽

nvcc 依 --std=c++<version> 旗標處理 CUDA 與 device code,支援 C++03/11/14/17/20/23 全部標準的所有語言特性,僅受各小節列出的限制約束。device code 與 host code 的最大差異集中在「執行期 / 並行 / 大型功能」:C++11 的 concurrency 機制(atomics、memory model、thread-local、例外傳播)在 device 上不支援,C++20 的 ModulesCoroutines 不支援。CUDA 另提供跨 host/device 的標準函式庫 libcu++

項目 重點
標準旗標 --std=c++03/11/14/17/20/23;傳入即開啟該版全部特性,並以對應 dialect 驅動 host preprocessor/compiler/linker
C++11 (CUDA 7.x) 語言特性幾乎全支援;concurrency 群組全不支援(atomics、memory model、thread-local、propagating exceptions)+ GC + extended integral types
C++14 (CUDA 9.x) Clarifying memory allocation (N3664) 與 Sized deallocation (N3778) 不支援
C++17 (CUDA 11.x) 列表內全部支援(structured bindings、constexpr if、constexpr lambda、fold expressions、inline variables…)
C++20 (CUDA 12.x) Concepts、operator<=>、consteval、constinit、char8_t 全支援Modules ×、Coroutines ×
C++23 多數支援(含 Deducing thisif consteval[[assume]]);少數 constexpr/比較/contextual-conversion 相關 DR 與特性不支援(5 項 ×、3 項 N/A)
標記語意 ✓ 支援 / × 不支援 / N/A 不適用於 device code(如 GC 移除、host-defined 行為)
libcu++ CUDA 版 C++ STL,host 與 device 皆可用;含 C++17 backport 與 extended type 支援

nvcc 的 C++ 標準支援總覽

nvcc 依下列規格處理 CUDA 與 device code,每個標準對應一個 --std 旗標:

標準 ISO/IEC 旗標 起始 CUDA Toolkit
C++03 14882:2003 --std=c++03
C++11 14882:2011 --std=c++11 7.x
C++14 14882:2014 --std=c++14 9.x
C++17 14882:2017 --std=c++17 11.x
C++20 14882:2020 --std=c++20 12.x
C++23 14882:2024 --std=c++23
表格判讀(適用 C++20/C++23 表)

DR: 前綴的項目是 Defect Report 決議:它們修正標準本身,也會套用到較早的 C++ 標準模式(如 C++17/C++20),列在此處只為完整性,並非該版專屬。NVCC 欄的 N/A 代表該特性不適用於 device code(例如移除無用的標準措辭如 GC 支援,或屬 host-defined 行為)。

C++11 device code 支援與限制

C++11 自 CUDA Toolkit 7.x 起支援。絕大多數語言特性可用:rvalue references、variadic templates、lambda expressions、auto-typed variables、Generalized constant expressions(即 constexpr 雛形)、strongly-typed enums、Range-based for、defaulted/deleted functions、Explicit virtual overrides__func__、C99 preprocessor、long long 等皆為 ✓。

device code 不支援的項目集中在「並行 / 執行期」相關,必須牢記:

未支援特性 提案 群組
Minimal support for garbage collection and reachability-based leak detection N2670
Sequence points N2239 Concurrency
Atomic operations N2427 Concurrency
Strong Compare and Exchange N2748 Concurrency
Bidirectional Fences N2752 Concurrency
Memory model N2429 Concurrency
Data-dependency ordering: atomics and memory model N2664 Concurrency
Propagating exceptions N2179 Concurrency
Allow atomics use in signal handlers N2547 Concurrency
Thread-local storage N2659 Concurrency
Dynamic initialization and destruction with concurrency N2660 Concurrency
Extended integral types N1988 C99 Features in C++11
device 沒有標準 concurrency 機制

整個 Concurrency 群組std::atomic、memory model、bidirectional fences、thread_local、例外傳播)在 device code 都不支援。CUDA 改以自家原語(atomic intrinsics、memory fence 函式、libcu++ 的 cuda::atomic 等)取代,並另有自己的記憶體一致性模型。

C++14 與 C++17 device code 支援

C++14(CUDA Toolkit 9.x)

幾乎全部支援:Functions with deduced return typegeneralized lambda capture (init-capture)generic (polymorphic) lambda、variable templates、Relaxing requirements on constexpr functions(放寬 constexpr 限制)、[[deprecated]]、digit separator(')等皆為 ✓。

device code 不支援僅兩項,皆與 operator new/delete 配置有關:

未支援特性 提案
Clarifying memory allocation N3664
Sized deallocation N3778

C++17(CUDA Toolkit 11.x)

來源列表內全部 ✓,包含常被考的現代特性:

C++17 是現代 CUDA 的安全基準

C++17 的所有語言特性在 device code 都可用,撰寫 kernel 時可放心使用 structured bindings、constexpr if、constexpr lambda 等。

C++20 device code 支援

C++20 自 CUDA Toolkit 12.x 起支援,並要求 host compiler 達到下列版本:

host compiler 最低版本
GCC ≥ 10.0
Clang ≥ 10.0
Microsoft Visual Studio ≥ 2022
nvc++ ≥ 20.7

device code 支援多數重量級特性:

device code 不支援的兩大功能:

未支援特性 提案
Modules P1103R3 / P1766R1 …(整組)
Coroutines P0912R5
C++20 的兩個「大功能」缺席

ModulesCoroutines 在 device code 不支援。其餘 C++20 語言特性(Concepts、<=>、consteval、constinit、char8_t…)皆可在 kernel 中使用。

C++23 device code 支援

C++23 的 host compiler 需求更高,且 MSVC 尚未支援

host compiler 最低版本
GCC ≥ 14.0
Clang ≥ 18.0
Microsoft Visual Studio Not Supported
nvc++ ≥ 24.3

device code 支援的代表性特性:Deducing this(P0847R7)、if consteval(P1938R3)、Multidimensional subscript operator[](P2128R6)、static operator() / static operator[]Portable assumptions [[assume]](P1774R8)、Extended floating-point types and standard names(P1467R9)、auto(x)/auto{x}、UTF-8 source file encoding、delimited escape sequences、#warning 等皆 ✓。

部分項目 device code 不支援(×) 或標為 N/A(不適用 device code)

項目 提案 狀態
Narrowing contextual conversions to bool P1401R5 ×
Relaxing some constexpr restrictions P2448R2 ×
DR: De-deprecating volatile bitwise compound assignment operations P2327R1 ×
DR: Using unknown pointers and references in constant expressions P2280R4 ×
DR: The Equality Operator You Are Looking For P2468R2 ×
Removing Garbage Collection Support P2186R2 N/A
Mixed string literal concatenation P2201R1 N/A
DR: Relax requirements on wchar_t to match existing practices P2460R2 N/A
× 與 N/A 的差別

× 表示 device code 確實不支援;N/A 表示該特性對 device code 本就不適用(如移除 GC 支援、host-defined 行為),並非「未實作」。

CUDA C++ Standard Library(libcu++)

CUDA 提供一份 C++ Standard Library(STL)實作,稱為 libcu++(標頭路徑置於 cuda/std/ 下,如 <cuda/std/...>;對應命名空間為 cuda::std)。其優勢:

libcu++ 另提供標準庫沒有、用以提升生產力與效能的 extended features

取得來源

libcu++ 隨 CUDA Toolkit 一同提供,同時也是開源 CCCL repository 的一部分。需要在 device code 使用標準庫設施(含對 device 不支援的 std::atomic/thread/memory model 等的替代品)時,優先用 libcu++。

考試/測驗重點

題型 關鍵答案
nvcc 支援哪些 C++ 標準? C++03/11/14/17/20/23,各對應 --std=c++NN
-std=c++NN 旗標做什麼? 開啟該版全部特性,並以對應 dialect 驅動 host preprocessor/compiler/linker
C++11 起始 Toolkit? CUDA Toolkit 7.x
C++11 device code 不支援哪一整組? Concurrency 群組:atomics、memory model、bidirectional fences、thread-local storage、propagating exceptions
C++11 還有哪些不支援? garbage collection 最小支援(N2670)、extended integral types(N1988)
thread_local 在 device 可用嗎? 不可用(Thread-local storage N2659 為 ×)
C++14 起始 Toolkit? CUDA Toolkit 9.x
C++14 不支援哪兩項? Clarifying memory allocation (N3664)、Sized deallocation (N3778)
C++17 起始 Toolkit? CUDA Toolkit 11.x
C++17 有不支援的語言特性嗎? 列表內全部支援(structured bindings、constexpr if、constexpr lambda、fold expressions、inline variables…)
C++20 起始 Toolkit? CUDA Toolkit 12.x
C++20 device code 不支援哪兩大功能? Modules 與 Coroutines
Concepts 在 device code 可用嗎? 可用(C++20,✓)
consteval / constinit / char8_t 可用嗎? 皆可用(C++20,✓)
C++20 的 host compiler 需求? GCC ≥ 10、Clang ≥ 10、MSVS ≥ 2022、nvc++ ≥ 20.7
C++23 的 host compiler 需求? GCC ≥ 14、Clang ≥ 18、MSVC Not Supported、nvc++ ≥ 24.3
Deducing this(P0847R7)device 可用嗎? 可用(C++23,✓)
C++23 device code 不支援哪些語言特性(×)? 5 項:Narrowing contextual conversions to bool(P1401R5)、Relaxing some constexpr restrictions(P2448R2)、De-deprecating volatile bitwise compound assignment(P2327R1)、Using unknown pointers/references in constant expressions(P2280R4)、The Equality Operator You Are Looking For(P2468R2)
C++23 表中 N/A 代表什麼? 該特性不適用於 device code(如 GC 移除、host-defined 行為),非未實作
DR: 前綴代表什麼? Defect Report 決議,修正標準並套用到較早 C++ 模式,非該版專屬
libcu++ 是什麼? CUDA 版 C++ Standard Library,host/device 皆可用
libcu++ 支援哪些 extended data types? __int128__half__nv_bfloat16__float128
libcu++ 提供哪種 backport? 將 C++20/23/26 標準庫特性 backport 到 C++17
libcu++ 從哪取得? 隨 CUDA Toolkit,亦在開源 CCCL repository