Date: Fri, 03 Jan 2020 23:46:46 +0300
TL;DR: Require users to specify all necessary concepts in template. This is, of course, incompatible change, so it should be implemented using epochs ( http://wg21.link/P1881 ). This is what templates should be right from the beginning. If this is not acceptable, then require programmers to specify all concepts if they specified at least one (this is not breaking change compared to C++17).
Hi.
Starting from their appearance templates has one very big disadvantage: a programmer doesn’t specify any constraints on template parameters. This means that, for example, the following code is valid, despite there is not known that T supports addition:
template <typename T> T add(T a, T b){ return a + b; } // Example #1
This means that it is hard for compiler to fully check templates for errors, and thus full error checking is deferred to template instantiation. Also this means that various tools (say, static analyzers) have no any information about that type «T». This is essentially duck-typing.
Compare this with Haskell. This is Haskell code, equivalent to above:
add :: t → t → t
add a b = a + b
And it doesn’t compile (unlike in C++), because programmer didn’t specify that T supports addition.
Fortunately, C++20 gives us something like a fix to this long-standing problem: concepts. Unfortunately, their use is not mandatory. I. e. you are not required to specify all concepts needed for this function (or class). Example #1 still compiles. You still have duck-typing. You still don’t have Haskell’s elegance. Compilers, analyzers, etc are still difficult to write.
So I propose to require specifying all needed concepts. This will make language stricter, safer, this will remove duck-typing. This will simplify writing compilers, tools and IDEs. When compiler sees template, it will be able to check it for all (!) errors. And when the compiler sees template instantiation, it will just check that types meet that concepts and nothing more! Compilation time will decrease.
Of course, this is breaking change. So, I propose this:
- Don’t add concepts in C++20 (this is optional step, i. e. still adopting concepts in C++20 is OK, too)
- Adopt epochs ( http://wg21.link/P1881 ) in C++23
- Add concepts to C++23 epoch and require programmer to specify all necessary concepts in C++23 epoch
This will be a great change which will make a language significantly better. This is how templates should look like right from the beginning!
Also, I understand that many people will not like my proposal. So, I propose an alternative: require all necessary concepts if programmer specified at least one. This is not breaking change, because it affects only code which uses concepts (but concepts don’t exist in C++17). And such change should be done simultaneously with introducing concepts, i. e. now.
==
Askar Safin
http://vk.com/safinaskar
Hi.
Starting from their appearance templates has one very big disadvantage: a programmer doesn’t specify any constraints on template parameters. This means that, for example, the following code is valid, despite there is not known that T supports addition:
template <typename T> T add(T a, T b){ return a + b; } // Example #1
This means that it is hard for compiler to fully check templates for errors, and thus full error checking is deferred to template instantiation. Also this means that various tools (say, static analyzers) have no any information about that type «T». This is essentially duck-typing.
Compare this with Haskell. This is Haskell code, equivalent to above:
add :: t → t → t
add a b = a + b
And it doesn’t compile (unlike in C++), because programmer didn’t specify that T supports addition.
Fortunately, C++20 gives us something like a fix to this long-standing problem: concepts. Unfortunately, their use is not mandatory. I. e. you are not required to specify all concepts needed for this function (or class). Example #1 still compiles. You still have duck-typing. You still don’t have Haskell’s elegance. Compilers, analyzers, etc are still difficult to write.
So I propose to require specifying all needed concepts. This will make language stricter, safer, this will remove duck-typing. This will simplify writing compilers, tools and IDEs. When compiler sees template, it will be able to check it for all (!) errors. And when the compiler sees template instantiation, it will just check that types meet that concepts and nothing more! Compilation time will decrease.
Of course, this is breaking change. So, I propose this:
- Don’t add concepts in C++20 (this is optional step, i. e. still adopting concepts in C++20 is OK, too)
- Adopt epochs ( http://wg21.link/P1881 ) in C++23
- Add concepts to C++23 epoch and require programmer to specify all necessary concepts in C++23 epoch
This will be a great change which will make a language significantly better. This is how templates should look like right from the beginning!
Also, I understand that many people will not like my proposal. So, I propose an alternative: require all necessary concepts if programmer specified at least one. This is not breaking change, because it affects only code which uses concepts (but concepts don’t exist in C++17). And such change should be done simultaneously with introducing concepts, i. e. now.
==
Askar Safin
http://vk.com/safinaskar
Received on 2020-01-03 14:49:19