c++ concepts 107

c++ concepts make c++ template programming funny, and make c++ template programming serious.

Declaring a c++ concept is to use the keyword concept, and with template declaraton.

template <typename type_target, typename type1, typename type2>
concept my_concept = an_expression_that_can_be_judged_true_or_false;

The first type type_target is the main type that will be constrained. The rest types (type1, type2, ...) are used to help to constrain the main type.

c++ example:

#include <iostream>

template <typename type_target>
namespace space
{
	concept has_type = requires ()
	{
		typename type_target::type;
	};
	class m_class
	{
	public:
		using type = int;
	};
	class n_class
	{
	public:
		using type_t = float;
	};
}
int main()
{
	std::cout << "c++, Hi!" << std::endl;
	space::has_type auto x = space::m_class{}; // OK: space::m_class has type.
	space::has_type auto y = space::n_class{}; // ERROR: space::n_class does not have type, it has type_t.
}

Sun May 25 12:38:06 PM UTC 2025


Up: c++