Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use concepts?

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;
}
like image 892
Hydrogen Avatar asked Aug 16 '21 09:08

Hydrogen


People also ask

How are concepts used?

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.

What are examples of concepts?

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.

What is a concept and how do I use it?

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.

What are conconcepts and how do they work?

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.

How do I get Started with concepts?

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.

What is a concept in research?

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.


Video Answer


1 Answers

So, two things.

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

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

like image 179
StoryTeller - Unslander Monica Avatar answered Oct 23 '22 11:10

StoryTeller - Unslander Monica