<url:
https://github.com/alf-p-steinbach/Expressive-Cpp>
Expressive C++ is a header only library. It implements a less unsafe,
more convenient and readable-for-non-C++-expert-folks dialect of C++. To
do this it depends on the following language extensions, which are
supported by g++, Visual C++ and CLang, and probably also by far more
compilers: • `__COUNTER__`, • `#pragma once`, • `#pragma push_macro`, •
`#pragma pop_macro`, and • use of the `$` character in identifiers.
The shortest possible Expressive C++ program is
$just{}
which is • shorter than a standard `main`, and • safer than a standard
`main` ¹in the case of an exception being thrown out of it, and more •
directly readable, without distracting bits such as the `int` in
int main(){}
And this is the general philosophy: ²not always shorter like here, but
safer and to the non-expert more directly readable, and generally more
convenient than the raw C++ that Expressive C++ code translates to.
Flavor ³example:
#include <p/expressive/use_weakly_all.hpp>
#include <iostream>
#include <vector> // std::vector
#include <string> // std::string
$use_weakly_all_from( std );
void cpp_main( ref_<const vector<string>> args )
{
for( $each arg $in enumerated( args ) )
cout << "Arg " << arg.index() << " is '" << arg.object() <<
"'.\n";
}
$start_with_ascii_arguments( cpp_main )
The `ref_` type builder, and others like it, allows one to use the
principle of substitution to construct types, as in non-C languages in
general. It also supports the practice of putting `const` first, even in
nested parts. The resulting type specifications can be naturally read in
the forward direction, unlike in raw C and C++.
The $ words like $each and $in above, are pseudo keywords, keywords for
the Expressive C++ dialect, implemented as macros. Expressive C++ also
offers some stuff implemented with ordinary C++ code, using C++ core
language features up to and including C++14. For example, the expression
$invoked{ $var x=1; while( x*x < 50 ) ++x; return x - 1; }
… uses an Expressive C++ pseudo keyword macro, `$invoked`, to produce a
lambda, and to pass it to some ordinary C++14 machinery that invokes
that lambda and produces its return value with the type implied by the
return statement – which e.g., as here, allows a loop in an expression.
Enjoy, even though this is just a version 0.3-or-something.
- Alf
Notes:
¹ If any statements are added, then unlike a raw `main` any exception
out of the `$just` block is caught and reported on the standard error
stream, with guaranteed stack rewinding/cleanup, and a suitable process
exit code that adheres to the OS conventions is produced.
² I've not yet found a good way to express the `:?` operator within the
limits of a macro approach.
³ Sorry about the limitation to “ascii” here. Implementing portable
Unicode command line support, which raw C++ `main` doesn't offer for
systems other than *nix, is much work, and I haven't got to that yet.