From b2f7575ef5bbb59f001f58ca0bfded074b3add8a Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 30 Aug 2020 10:49:33 -0400 Subject: [PATCH] Add some alternate forms for `const T a` --- _posts/2020-08-05-static-polymorphism.md | 42 ++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/_posts/2020-08-05-static-polymorphism.md b/_posts/2020-08-05-static-polymorphism.md index b8dda6f..b563924 100644 --- a/_posts/2020-08-05-static-polymorphism.md +++ b/_posts/2020-08-05-static-polymorphism.md @@ -219,8 +219,46 @@ int main() { } ``` -May be better off defining `const T` methods in one concept, `T` methods in another, and then having -one concept that `requires` the sub-concepts, but just trying to demonstrate what is possible. +Alternate alternate form: + +```c++ +template +concept ConstMethod = requires { + requires requires (const T a) { + { a.const_method() } -> std::same_as; + }; + + requires requires (T a) { + { a.nonconst_method() } -> std::same_as; + { a.unnecessary_const_method() } -> std::same_as; + }; +}; +``` + +Third alternate form: + +```c++ +template +concept ConstMethods = requires (const T a) { + { a.const_method() } -> std::same_as; + }; + +template +concept NonConstMethods = requires (T a) { + { a.nonconst_method() } -> std::same_as; + { a.unnecessary_const_method() } -> std::same_as; + }; + + +template +concept ConstMethod = requires { + requires ConstMethods; + requires NonConstMethods; +}; +``` + +...which goes a long way towards explaining why the "requires requires" form is necessary. Not sure +what the "best practices" form is, just trying to demonstrate what is possible. Working with `const` parameters can be a bit weird because of implicit copies: