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 的 Modules 與 Coroutines 不支援。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 this、if 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 |
— |
- 傳入
nvcc -std=c++<version>會開啟該版相關的所有 C++ 特性,並以對應的 C++ dialect 選項一併驅動 host 的 preprocessor、compiler 與 linker。 - compiler 支援所支援標準的所有語言特性,但受後續各小節列出的限制約束。
標 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 |
整個 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 type、generalized 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)
來源列表內全部 ✓,包含常被考的現代特性:
- Structured bindings(P0217R3)
constexpr if(P0292R2)、constexpr lambda(P0170R1)- Fold expressions(折疊運算式,N4295)與 unary folds(P0036R0)
- Inline variables(P0386R2)
- Class template argument deduction(CTAD,P0091R3 / P0512R0)
Selection statements with initializer、Lambda capture of *this by Value、[[fallthrough]]/[[nodiscard]]/[[maybe_unused]]、guaranteed copy elision、__has_include等
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 支援多數重量級特性:
- Concepts(P0734R0 等一系列提案)✓
- Consistent comparison
operator<=>(三向比較,P0515R3 等)✓ - Immediate functions
consteval(P1073R3)、std::is_constant_evaluated(P0595R2)✓ constinit(P1143R2)、char8_t(P0482R6)、using enum(P1099R5)✓- Designated initializers、
__VA_OPT__、[=, this]捕獲、[[likely]]/[[unlikely]]、explicit(bool)、class types 作為 NTTP、[[nodiscard("reason")]]等皆 ✓ - 多項
Relaxations of constexpr restrictions(P1002R1 等)✓
device code 不支援的兩大功能:
| 未支援特性 | 提案 |
|---|---|
| Modules | P1103R3 / P1766R1 …(整組) |
| Coroutines | P0912R5 |
Modules 與 Coroutines 在 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 |
× 表示 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)。其優勢:
- host 與 device 皆可用:同一套設施在主機與裝置上都能呼叫。
- 相容 CUDA Toolkit 支援的所有 Linux 與 Windows 平台。
- 相容最近兩個主要版本的 CUDA Toolkit 所支援的所有 GPU 架構。
- 相容當前與前一個主要版本的所有 CUDA Toolkit。
- 提供近期標準(C++20、C++23、C++26)部分標準庫特性的 C++17 backport。
- 支援 extended data types:128-bit 整數(
__int128)、半精度浮點(__half)、Bfloat16(__nv_bfloat16)、四倍精度浮點(__float128)。 - 針對 device code 高度最佳化。
libcu++ 另提供標準庫沒有、用以提升生產力與效能的 extended features:
- 數學函式(mathematical functions)
- 記憶體操作(memory operations)
- 同步原語(synchronization primitives)
- 容器擴充(container extensions)
- CUDA intrinsics 的高階抽象、C++ PTX wrappers 等
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 |