Currently, I am learning C++ and decided just start with C++20. However, these codes are driving me crazy, since I don't think the result makes any sense.
The following code will have the sentence Valid array.
printed. What I meant above is that this is not right. It shouldn't print the sentence at all, since the type I inserted in the parameter doesn't match the concept.
Tested on VS2022 Preview 3 and an online compiler with newest GCC and C++2A(GNU) arguments, generated the same results.
#include <array>
#include <cstdio>
#include <iostream>
#include <type_traits>
using namespace std;
template<typename A> concept ProperArray = requires(A array)
{
{array.max_size() >= 2U};
{std::is_arithmetic_v<typename A::value_type> == true};
};
int main()
{
constexpr bool b = ProperArray<array<std::string, 1>>;
if constexpr (b)
cout << "Valid array." << endl;
std::cout << "Hello, Wandbox!" << std::endl;
}
Concepts are used as formal tools or models in mathematics, computer science, databases and artificial intelligence where they are sometimes called classes, schema or categories. In informal use the word concept often just means any idea.
Concepts can be based on real phenomena and are a generalized idea of something of meaning. Examples of concepts include common demographic measures: Income, Age, Eduction Level, Number of SIblings.
Read on and learn how to use them in your code! What is a concept? In short, a concept is a set of constraints on template parameters evaluated at compile time. You can use them for class templates and function templates to control function overloads and partial specialization.
Concepts are a revolutionary approach for writing templates! They allow you to put constraints on template parameters that improve the readability of code, speed up compilation time, and give better error messages.
Get started using Concepts' Shape Guides, Snap, Selection and Layers as you design a simple object. The world is full of design problems to conquer, like home improvements, life-simplifying gadgets, personal style and the quest for artificial intelligence.
A concept is like an umbrella under which many ideas can be lined up. Many different ideas can be generated to give life to it. This is what makes concepts so powerful; there are many ways to act upon a concept. At the same time, you can use the concept to assess each and every idea.
So, two things.
You are using simple requirements (the extra {}
make those compound requirements technically, but since you don't use any optional feature of those, it's equivalent to a simple requirement). Those mostly verify that an expression is syntactically valid. Its value is immaterial to them. What you want is nested requirements:
template<typename A> concept ProperArray = requires(A array)
{
requires array.max_size() >= 2U;
requires std::is_arithmetic<typename A::value_type>::value;
};
These requirements (whose expression must be a constant expression), check the value indeed. Mind however, that if max_size
is not a constexpr
or consteval
member function, or if the evaluation would not produce a constant expression, then it's a hard error. The concept won't simply be "false".
if constexpr
outside of a template is moot. It doesn't do the same discarding magic it does when its condition is value-dependent on a template parameter. You could have used a regular if
to the same effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With