0% found this document useful (0 votes)
33 views6 pages

C++20 Concepts Guide

C++ Concept

Uploaded by

Hariom Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

C++20 Concepts Guide

C++ Concept

Uploaded by

Hariom Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++20 Concepts: A Comprehensive Guide

1. Introduction to C++20 Concepts

C++20 introduced concepts to provide a way to specify constraints on template parameters. They

enhance code readability, error messages, and maintainability by offering a mechanism to

express intent directly in the code.

Key Features:

- Improves Code Clarity: Concepts describe template requirements explicitly.

- Better Compiler Diagnostics: Error messages for templates are more intuitive.

- SFINAE Replacement: Simplifies the need for complex std::enable_if expressions.

Syntax:

template<typename T>

concept ConceptName = /* boolean expression */;

Example:

#include <concepts>

#include <iostream>

template<typename T>

concept Incrementable = requires(T a) {

a++;

};
void printIncrementable(Incrementable auto value) {

std::cout << value << std::endl;

int main() {

int x = 10;

printIncrementable(x); // Valid

// printIncrementable("test"); // Compilation error

return 0;

}
2. Common Concepts in the Standard Library

C++20 provides pre-defined concepts in the standard library for common use cases.

Commonly Used Concepts:

1. std::integral: Ensures the type is an integral (integer) type.

Example:

#include <concepts>

void printIfIntegral(std::integral auto value) {

std::cout << value << std::endl;

2. std::floating_point: Ensures the type is a floating-point type.

3. std::same_as<T, U>: Ensures that two types are the same.

4. std::convertible_to<T, U>: Ensures one type is convertible to another.

Example:

#include <concepts>

#include <iostream>

void acceptIntegral(std::integral auto value) {

std::cout << "Integral: " << value << std::endl;

}
void acceptFloatingPoint(std::floating_point auto value) {

std::cout << "Floating-point: " << value << std::endl;

int main() {

acceptIntegral(42); // Works

// acceptIntegral(3.14); // Compilation error

acceptFloatingPoint(3.14); // Works

return 0;

}
3. Writing Custom Concepts

Creating custom concepts allows you to define specific constraints for template parameters.

This provides more flexibility and control in generic programming.

Steps to Define a Custom Concept:

1. Write a boolean expression that evaluates the constraints.

2. Use requires to specify requirements like valid expressions, member functions, etc.

Custom Concept Example:

#include <concepts>

#include <iostream>

// Define a concept to check if a type supports addition

template<typename T>

concept Addable = requires(T a, T b) {

{ a + b } -> std::same_as<T>;

};

template<Addable T>

T add(T a, T b) {

return a + b;

int main() {

std::cout << add(3, 4) << std::endl; // Works


// std::cout << add("hello", "world"); // Compilation error

return 0;

Using requires for Advanced Constraints:

template<typename T>

concept ComplexConstraint = requires(T x) {

{ x + x } -> std::same_as<T>;

{ x * x } -> std::same_as<T>;

x == x;

x != x;

};

You might also like