3 ms·
Looks neat. Of course you can do this in c++.
by ant6n 4y ago
Looks neat. Of course you can do this in c++.
- SAI_Peregrinus 4y agoOf course, templates are sufficiently complex metaprogramming to achieve this. But it's not a language built-in, or even part of the standard library. F# has a refinement type system built in (which allows this), and of course Haskell and Idris are sufficiently capable in their type systems to trivially support it, but none of those are particularly mainstream general-purpose programming languages. Mainstream languages pretty much all need some sort of library for it, which is unfortunate since they're extremely useful in many common programming problems.
- ant6n 4y agoIn C++, you can do this with custom literals, not meta programming: https://en.cppreference.com/w/cpp/language/user_literal https://en.cppreference.com/w/cpp/language/user_literal One would have to make a library with various classes representing various units, plus the (implicit) conversion between them. It´s probably something that exists somewhere.