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#

  1. 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.

  2. All user-defined names for entities should use snake_case, except for template parameters, which use PascalCase, and macros, which use ALL_CAPS.

  3. Headers must use #pragma once over include guards, except for libcudacxx and cudax.

  4. 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.

  5. 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.

  6. 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.

  7. Remove any unused files, code, or entities like variables, functions, types, template parameters, etc.

Header inclusion#

  1. 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.

  2. Don’t include any unneeded headers, since it unnecessarily increases compile time.

  3. 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.

  4. If possible, prefer forward declaration or a forward declaring header like __fwd/header.h over including the implementation header.

  5. All header inclusions must use angle brackets, e.g. <path/header>. Relative includes using quotes are only allowed in tests, examples, and benchmarks.

  6. Never include headers from cuda/std/__cccl/* directly, include __cccl_config instead. cuda/std/__cccl/prologue.h and cuda/std/__cccl/epilogue.h are only allowed in library headers.

  7. Never include headers from cuda/std/__internal/* directly, include cuda/std/detail/__config instead.

  8. Never include headers from thread/detail/config/* directly, include thrust/detail/config.h instead.

Header removal#

  1. 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#

  1. Always prefer entities from cuda::std:: over std:: 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.

  2. 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 called cuda themselves. Use just cuda::std:: etc. in examples, tests, benchmarks, and documentation.

  3. 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 just std:: in examples, tests, benchmarks, and documentation.

  4. A local using declaration, e.g. using ::cuda::std::remove_reference_t;, is acceptable to avoid repetition within a function body.

  5. Some functions, like ::cuda::std::swap and ::cuda::std::get, may be called unqualified, when they follow a preceding using declaration, e.g. using ::cuda::std::get;.

Variables#

  1. Variables which are not modified must be declared const. This does not apply to function parameters.

  2. Variables with an initializer that can be evaluated at compile-time must be declared constexpr.

  3. constexpr variables and variable templates at namespace/global scope must be declared inline.

  4. Use uniform initialization to call constructors (class types only) and compile-time conversions, e.g. constexpr auto x = int{sizeof(float)};.

  5. Prefer plural names for variables of array, span, list types, e.g. int values[4] instead of int value[4].

Functions#

  1. Non-template, non-constexpr functions must be declared inline.

  2. Functions with a non-void return type should use [[nodiscard]].

  3. Declare a function _CCCL_CONSTEVAL when it can only be evaluated at compile time.

  4. Prefer C++20 concept macros (_CCCL_TEMPLATE(...) and _CCCL_REQUIRES(...)) instead of SFINAE (enable_if).

  5. 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.

  6. 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#

  1. Use CCCL internal macros over compiler/vendor-specific keywords in library implementation headers. E.g., use _CCCL_HOST_DEVICE instead of __host__ __device__, or _CCCL_FORCEINLINE over __forceinline. Generally, prefer _CCCL_HOST_API, _CCCL_DEVICE_API, _CCCL_HOST_DEVICE_API, _CCCL_TILE_API, or _CCCL_API for 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_DEVICE may be required for tests targeting non-CUDA backends.

  2. Macros for function attributes, like _CCCL_HOST_DEVICE, should be ordered before declaration specifiers like constexpr or static

  3. Do not mark kernels with __global__ but with _CCCL_KERNEL_ATTRIBUTES

Comments#

  1. Doxygen comments should start with //!

  2. In Doxygen comments, we prefer the use of @ to start Doxygen commands.

  3. In Doxygen comments, prefer @c when referring to code entities

  4. Comments 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.

  5. Commented-out code without a justification is not allowed.

  6. Every entity in the public API must have Doxygen documentation, at least a @brief description.

  7. Documentation must accurately reflect the entity’s current behavior.

Using CUDA APIs#

  1. In headers, all calls to CUDA Runtime (cudaXxx(...)) and CUDA Driver (cuXxx(...) or cuda::__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_API or _CCCL_ASSERT_RUNTIME_API macros. CUDA Driver calls should prefer using the _CCCL_TRY_DRIVER_API or _CCCL_ASSERT_DRIVER_API macros.

  2. 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).

Compiler Compatibility#

  1. Guard host-only code with #if !_CCCL_COMPILER(NVRTC) or #if _CCCL_HOSTED().

  2. 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.

  1. Any entity inside the namespace cuda, including all nested namespaces, unless the entity is prefixed with _, is considered part of the public API.

  2. All user-defined names for entities which are not part of the public API must be prefixed with __ when they use snake_case, and with _ when they use PascalCase or ALL_CAPS. This turns them into C++ reserved identifiers to avoid name collisions with user code and macros.

  3. Avoid single-letter template parameter names. Wrong: _T; correct: _Tp.

  4. Data member names must be postfixed by _, e.g. class __myclass { int __data_; };.

  5. 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.

  6. Always fully qualify type names unless they are declared in the current namespace or an enclosing one.

  7. Defaulted constructors should be marked with _CCCL_HIDE_FROM_ABI

  8. libcu++ headers like <cuda/foo> are strict supersets of <cuda/std/foo> and thus always include the corresponding <cuda/std/...> header.

  9. Constructor parameter names should match class data member names without the postfix _, e.g. class __myclass { __myclass(int __data) : __data_(__data) {} };.

  10. Headers use include guards with names derived from the uppercase full path and closing #endif comments repeating the guard name.

  11. 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
    
  12. 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.

  13. Functions that do not throw exceptions must use noexcept.

  14. Functions that can be evaluated at compile time should be constexpr. If this requires a newer C++ standard, use _CCCL_CONSTEXPR_CXX20 or _CCCL_CONSTEXPR_CXX23. Since constexpr is a commitment for public APIs, authors may decline to add it with a justification.

CUB and Thrust#

  1. Any entity inside the namespace cub or thrust, including all nested namespaces, unless any namespace is named detail, or the entity is prefixed with __, is considered part of the public API. This exemption does not apply if a detail entity is re-exposed through a public alias or base class outside detail later; such re-exposed entities remain part of the public API.

  2. Non-public entities, except macros, should be put inside a detail namespace (preferred) or prefixed with __. Non-public macros should be prefixed with _.

CUB#

  1. Entities in the public API, which are not macros, must be written in PascalCase to stay consistent with existing practice.

  2. Unit tests must use c2h::[host|device]_vector as containers over Thrust vectors, since they handle OOM conditions gracefully on some testing systems.

  3. Header files use .cuh as file extension

  4. Always 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#

  1. Header files use .h as file extension

  2. Any 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.

  1. Any net new file should be licensed under Apache-2.0 WITH LLVM-exception.

  2. 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
    
  3. 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.
    //
    //===----------------------------------------------------------------------===//
    
  4. 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.
    //
    //===----------------------------------------------------------------------===//