1

I understand the definition of two templates.
But I don't know under what circumstances this would actually be useful.
Could you show an example code where it is used under what circumstances?

Remark

ysbaekFox
  • 93
  • 7
  • 2
    Often in a function we assume that the input variables have all the default methods (assignment copy, etc), so we would put `semiregular`, if we also wanted equality then we'd put `regular`. I'm stating the obvious here... but that's what they're for. Just look at your function/method, and if it uses the variables' methods in such a way that requires them, then add that `requires`, otherwise don't. – Elliott Nov 15 '21 at 00:21
  • 3
    You mention "templates", but you don't mention the term _concepts_ -- which is what these are. Are you familiar with C++20 concepts, and what makes them useful? If not, are you familiar with SFINAE? – Drew Dormann Nov 15 '21 at 00:26
  • @Drew Dormann I'm not familiar with C++20, and also SFINAE.. I will study that you said, If you recommend to me article? – ysbaekFox Nov 15 '21 at 00:32
  • @ Elliott Thank you, I understand what you are saying. :) – ysbaekFox Nov 15 '21 at 00:34
  • 3
    If you look at the links you provide, these templates are not classes and not functions, they are each a `concept`. You can read up on the purpose of concepts [at this question](https://stackoverflow.com/questions/15669592/what-are-the-differences-between-concepts-and-template-constraints). – Drew Dormann Nov 15 '21 at 00:37

1 Answers1

1

I think in a really brief way this is the difference between them:

std::semiregular - > a semiregular type has to support the rule of five: and has to be swappable

std::regular -> same as semiregular, but you requires that the type is equality comparable

For both of them you already have simple examples how they work (the links on the question).

Now, how I see this useful in the projects. You probably can use this concept to check if your object, when passed as template type, respect the rule of five - so you can use semiregular for this. The example I gave is a very generic one, but definitely should be more. Take a look at the following link which has more details about this: https://www.modernescpp.com/index.php/c-20-define-the-concept-regular-and-semiregular .

Malak
  • 13
  • 4
  • 2
    Instead of saying *"supports the rule of five"*, it's better to say *"copyable"*. One might argue that deleting copy/move operations does conform to the rule of five. – HolyBlackCat Jan 30 '23 at 11:23