wiki:C++Features

Version 4 (modified by Eric Pettersen, 2 months ago) ( diff )

--

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) {

Initializer Lists

For initializing containers:

static std::vector<std::string> symbols = { "&", "|" };

​Discussion

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)---"

​More discussion

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]);
    };
Note: See TracWiki for help on using the wiki.