CCCL C++ Coding Conventions#
The following guidelines must generally be followed when contributing to any of the CCCL C++ libraries, including tests, benchmarks, examples and utility libraries. General guidelines apply universally, but may be overridden by library-specific or path-specific guidelines.
General#
Use the latest C++ features available. The repository supports C++17 but many newer library features are available through backports. Exception: the headers under
libcudacxx/include/nv/must keep compiling as C++11 for downstream consumers.All user-defined names for entities should use
snake_case, except for template parameters, which usePascalCase, and macros, which useALL_CAPS.Headers must use
#pragma onceover include guards, except for libcudacxx and cudax.API breaking changes of any public entity must be avoided, unless the next release will be a major release. If in doubt, consult a library maintainer. A change is breaking if it can lead to a compilation failure in any consumer code using CCCL, or change the behavior or meaning of user code in any other way.
Before an API is removed at a major release, the affected entity must be marked as
[[deprecated("...")]]with rationale and workaround, adding a Doxygen comment//! Deprecated [Since X.Y]with the CCCL version introducing the deprecation, in at least one preceding release.Do not name entities after identifiers that compilers or system headers define as macros, such as
__in,__out,__inout,__input,__output,__acquire,__release, or__bound.Remove any unused files, code, or entities like variables, functions, types, template parameters, etc.
Header inclusion#
Any source file must include all headers providing the symbols that they are using. Relying on transitive header inclusion is not allowed, as it leads to fragile code and frequent breakage during refactoring.
Don’t include any unneeded headers, since it unnecessarily increases compile time.
Prefer including the smallest possible libcu++ detail headers (starting with
__) to reduce compile time. E.g.#include <cuda/std/__type_traits/is_array.h>instead of#include <cuda/std/type_traits>. In tests and examples, always use the public headers.If possible, prefer forward declaration or a forward declaring header like
__fwd/header.hover including the implementation header.All header inclusions must use angle brackets, e.g.
<path/header>. Relative includes using quotes are only allowed in tests, examples, and benchmarks.Never include headers from
cuda/std/__cccl/*directly, include__cccl_configinstead.cuda/std/__cccl/prologue.handcuda/std/__cccl/epilogue.hare only allowed in library headers.Never include headers from
cuda/std/__internal/*directly, includecuda/std/detail/__configinstead.Never include headers from
thread/detail/config/*directly, includethrust/detail/config.hinstead.
Header removal#
Accidental header removal can cause tests to pick up a stale copy of the same header from a system or CUDA Toolkit installation, masking the deletion. Commits that delete or move a product header must say so clearly in the commit message so internal testing can treat the change as intentional. The mention may appear in the subject or the body. For example:
[libcu++] Some change Reason for the changes. Removed obsolete header <cuda/std/__foo/bar.h> Renamed header <cuda/std/__foo/meow.h> to <cuda/std/__foo/bark.h>
This requirement applies only to product code under:
cub/cub/thrust/thrust/libcudacxx/include/cuda/libcudacxx/include/nv/
Qualification#
Always prefer entities from
cuda::std::overstd::and other functionality or macros from the<cuda/...>and<cuda/std/...>headers. They generally work in host and device code, often work with NVRTC, and help testing our implementation. An exception can be made to compute reference results in unit tests, where calling the host standard library is acceptable.Fully qualify any reference to a libcu++ entity in any C++ library header with
::cuda::std::,::cuda::, etc. This avoids ambiguities when users define namespaces calledcudathemselves. Use justcuda::std::etc. in examples, tests, benchmarks, and documentation.Fully qualify any references to a host standard library entity in any C++ library header with
::std::. This avoids ambiguities with entities from::cuda::std::. Use juststd::in examples, tests, benchmarks, and documentation.A local
usingdeclaration, e.g.using ::cuda::std::remove_reference_t;, is acceptable to avoid repetition within a function body.Some functions, like
::cuda::std::swapand::cuda::std::get, may be called unqualified, when they follow a preceding using declaration, e.g.using ::cuda::std::get;.
Variables#
Variables which are not modified must be declared
const. This does not apply to function parameters.Variables with an initializer that can be evaluated at compile-time must be declared
constexpr.constexprvariables and variable templates at namespace/global scope must be declaredinline.Use uniform initialization to call constructors (class types only) and compile-time conversions, e.g. constexpr auto x = int{sizeof(float)};.
Prefer plural names for variables of array, span, list types, e.g.
int values[4]instead ofint value[4].
Functions#
Non-template, non-
constexprfunctions must be declaredinline.Functions with a non-void return type should use [[nodiscard]].
Declare a function
_CCCL_CONSTEVALwhen it can only be evaluated at compile time.Prefer C++20 concept macros (
_CCCL_TEMPLATE(...)and_CCCL_REQUIRES(...)) instead of SFINAE (enable_if).Avoid unconstrained deduced return types (
auto,decltype(auto)). Use an explicit return type or a concept-constrained placeholder. If an unconstrained deduced return type is necessary, add a comment explaining why.Do not use extended lambda expressions (lambdas annotated with
__host__/__device__etc.), except in examples and API example tests (*_api.cu), since they require that users add an extra flag (--extended-lambda).
Macros#
Use CCCL internal macros over compiler/vendor-specific keywords in library implementation headers. E.g., use
_CCCL_HOST_DEVICEinstead of__host__ __device__, or_CCCL_FORCEINLINEover__forceinline. Generally, prefer_CCCL_HOST_API,_CCCL_DEVICE_API,_CCCL_HOST_DEVICE_API,_CCCL_TILE_API, or_CCCL_APIfor any function inside CCCL. The same applies to compiler and compilation-mode detection in preprocessor conditions: use_CCCL_CUDA_COMPILATION(),_CCCL_DEVICE_COMPILATION(), and_CCCL_CUDA_COMPILER(...)instead of raw__CUDACC__,__CUDA_ARCH__, or__NVCOMPILER, and_CCCL_COMPILER(...)instead of raw__GNUC__,__clang__, or_MSC_VER. Examples and documentation must not use these macros and should support vendor attributes and keywords instead. Tests should only use macros if they are strictly required for the test to work. For instance,_CCCL_HOST_DEVICEmay be required for tests targeting non-CUDA backends.Macros for function attributes, like
_CCCL_HOST_DEVICE, should be ordered before declaration specifiers likeconstexprorstaticDo not mark kernels with
__global__but with_CCCL_KERNEL_ATTRIBUTES
Using CUDA APIs#
In headers, all calls to CUDA Runtime (
cudaXxx(...)) and CUDA Driver (cuXxx(...)orcuda::__driver::xxxNoThrow(...)) APIs must check the returned value or explicitly ignore it with a comment containing a justification. CUDA Runtime calls should prefer using the_CCCL_TRY_RUNTIME_APIor_CCCL_ASSERT_RUNTIME_APImacros. CUDA Driver calls should prefer using the_CCCL_TRY_DRIVER_APIor_CCCL_ASSERT_DRIVER_APImacros.- In tests, all CUDA Runtime/Driver calls must check the returned value manually, using
assert(lit-style tests), or using
(CHECK|REQUIRE)_(CUDA|CUDART)macros (catch2-style tests).
- In tests, all CUDA Runtime/Driver calls must check the returned value manually, using
Compiler Compatibility#
Guard host-only code with
#if !_CCCL_COMPILER(NVRTC)or#if _CCCL_HOSTED().In headers, do not rely on class template argument deduction (CTAD) or deduction guides; spell out the template arguments. Older compilers have CTAD bugs. Examples and tests may use CTAD.
libcu++#
These rules also include cudax.
Any entity inside the namespace
cuda, including all nested namespaces, unless the entity is prefixed with_, is considered part of the public API.All user-defined names for entities which are not part of the public API must be prefixed with
__when they usesnake_case, and with_when they usePascalCaseorALL_CAPS. This turns them into C++ reserved identifiers to avoid name collisions with user code and macros.Avoid single-letter template parameter names. Wrong:
_T; correct:_Tp.Data member names must be postfixed by
_, e.g.class __myclass { int __data_; };.Always fully qualify calls to free functions, even to functions in the same namespace. This avoids ADL. This does not apply to examples, tests, benchmarks, and documentation.
Always fully qualify type names unless they are declared in the current namespace or an enclosing one.
Defaulted constructors should be marked with
_CCCL_HIDE_FROM_ABIlibcu++ headers like
<cuda/foo>are strict supersets of<cuda/std/foo>and thus always include the corresponding<cuda/std/...>header.Constructor parameter names should match class data member names without the postfix
_, e.g.class __myclass { __myclass(int __data) : __data_(__data) {} };.Headers use include guards with names derived from the uppercase full path and closing
#endifcomments repeating the guard name.Right after the include guard, include:
#include <cuda/std/detail/__config> #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) # pragma GCC system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) # pragma clang system_header #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) # pragma system_header #endif // no system header
The last included header before the start of the code must be
<cuda/std/__cccl/prologue.h>, and<cuda/std/__cccl/epilogue.h>must appear after the code at the end of the file.Functions that do not throw exceptions must use
noexcept.Functions that can be evaluated at compile time should be
constexpr. If this requires a newer C++ standard, use_CCCL_CONSTEXPR_CXX20or_CCCL_CONSTEXPR_CXX23. Sinceconstexpris a commitment for public APIs, authors may decline to add it with a justification.
CUB and Thrust#
Any entity inside the namespace
cuborthrust, including all nested namespaces, unless any namespace is nameddetail, or the entity is prefixed with__, is considered part of the public API. This exemption does not apply if adetailentity is re-exposed through a public alias or base class outsidedetaillater; such re-exposed entities remain part of the public API.Non-public entities, except macros, should be put inside a
detailnamespace (preferred) or prefixed with__. Non-public macros should be prefixed with_.
CUB#
Entities in the public API, which are not macros, must be written in
PascalCaseto stay consistent with existing practice.Unit tests must use
c2h::[host|device]_vectoras containers over Thrust vectors, since they handle OOM conditions gracefully on some testing systems.Header files use
.cuhas file extensionAlways fully qualify calls to free functions, even to functions in the same namespace. This avoids ADL. Use
_CUB::instead of::cub::for the top level CUB namespace. This does not apply to examples, tests, benchmarks, and documentation.
Thrust#
Header files use
.has file extensionAny full qualification of a Thrust entity must use
THRUST_NS_QUALIFIER::instead of::thrust::. This avoids lookup failures when a user defines a wrapped namespace.
Licensing#
Every source or header file requires a license header at the top of the file. CCCL uses a mix of different licenses for historical reasons.
Any net new file should be licensed under Apache-2.0 WITH LLVM-exception.
For files with license headers, except in libcu++, strongly prefer a 2-line SPDX header like:
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
libcu++ source files ported from LLVM libc++ use the LLVM license header:
//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===//
All other libcu++ source files use the following license header:
//===----------------------------------------------------------------------===// // // Part of libcu++, the C++ Standard Library for your entire system, // under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. // //===----------------------------------------------------------------------===//
Comments#
Doxygen comments should start with
//!In Doxygen comments, we prefer the use of
@to start Doxygen commands.In Doxygen comments, prefer
@cwhen referring to code entitiesComments should express what the code cannot say. They complement code. Before writing an explanatory comment, consider whether refactoring the code could allow the code to express the same. This avoids code and comments getting out of sync.
Commented-out code without a justification is not allowed.
Every entity in the public API must have Doxygen documentation, at least a
@briefdescription.Documentation must accurately reflect the entity’s current behavior.