Our Friend Otto auto
Simplify declarations; examples...
auto err_val = PyList_New(2);
for (auto node: ast.nodes) {
auto distance = (ast.choice == 0 ? eval_real_number : eval_integer)(*ast.nodes[1]);
for (auto i = 1u; I < ast.nodes.size(); ++i) {
Decltype
Same purpose as typedef, ensure declarations have the same type, but sometimes more convenient, or better if the type isn't something you have direct control over:
auto list_size = PySequence_Fast_GET_SIZE(models); for (decltype(list_size) i = 0; i < list_size; ++i)
Initializer Lists
For initializing containers:
static std::vector<std::string> symbols = { "&", "|" };
Raw Strings
For more easily specifying strings that have lots of special characters (think newlines and backslashes) such as regular expressions or parser grammars.
The basic syntax is:
R"(some complex string)"
Since in some situations the sequence )" might occur in the string, additional characters can be between the R and the ", e.g.:
R---"(some complex string)---"
Humongous Lambdas
It just didn't occur to me that lambdas could be more than short anonymous functions handled off as arguments to other functions, e.g.:
// as_term spec_parser["as_term"] = [](const SemanticValues &vs) { auto objects_inst = std::any_cast<PyObject*>(vs[0]); if (vs.choice() == 1) { // tilde auto ret = PyObject_CallMethodObjArgs(objects_inst, invert_arg, session, models, nullptr); if (ret == nullptr) throw std::logic_error(use_python_error); Py_DECREF(ret); outermost_inversion = true; } else outermost_inversion = false; if (vs.size() == 1) return objects_inst; // there's a zone selector return process_zone(std::any_cast<PyObject*>(vs[0]), vs[1]); };
Any type
Holds a copy of a value and its type.
#include <any> #include <iostream> #include <string> int main() { std::any a; // Empty std::any a = 42; // Stores an int std::cout << "Value: " << std::any_cast<int>(a) << std::endl; a = "Hello, C++!"; // Stores a const char* std::cout << "Value: " << std::any_cast<const char*>(a) << std::endl; a = std::string("Dynamic string"); // Stores a std::string std::cout << "Value: " << std::any_cast<std::string>(a) << std::endl; try { // Attempting to cast to the wrong type will throw an exception std::cout << std::any_cast<double>(a) << std::endl; } catch (const std::bad_any_cast& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; }