-
Notifications
You must be signed in to change notification settings - Fork 294
Cpp Coding Guidelines
Bernhard Manfred Gruber edited this page Nov 4, 2025
·
19 revisions
- Always use entities from
cuda::std::overstd::. They work in host and device code, work with NVRTC, and help testing our implementation. - Use CCCL internal macros over compiler/vendor-specific keywords in implementation headers. E.g., use
_CCCL_HOST_DEVICEinstead of__host__ __device_, or_CCCL_FORCEINLINEover__forceinline. 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. - 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::stdin examples, tests and documentation. - Doxygen comments should start with
//! - In documentation comments, we prefer the use of
@cwhen referring to code entities and@briefand so on for doxygen commands - Macros for function attributes, like
_CCCL_HOST_DEVICE, should be ordered before declaration specifiers likeconstexprorstatic - Include libcu++ detail headers (starting with
__) also in downstream projects (like CUB, Thrust, etc.) to reduce compile time. In tests and examples, always prefer the public headers. - 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.
- Always fully qualify function calls, even to functions in the same namespace. This avoids ADL.
- Defaulted constructors should be marked with
_CCCL_HIDE_FROM_ABI - libcu++ headers like
<cuda/foo>are strict supersets of<cuda/std/foo>and thus always include the corresponding<cuda/std/...>header.
- All user-defined names for entities should use
snake_case, except for template parameters, which usePascalCase, and macros, which useALL_CAPS.
- Unit tests must use
c2h::[host|device]_vectoras containers over Thrust vectors, since they handle OOM conditions gracefully on some testing systems.
CCCL uses a mix of different licenses for historical reasons.
- Any net new file should be licsensed under Apache-2.0 WITH LLVM-exception.
- For files with license headers, strongly prefer a 2-line SPDX header like:
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception