From CppNow 2013
bearophile
bearophileHUGS at lycos.com
Wed May 22 14:11:02 PDT 2013
Among the CppNow 2013 slide packs I have found two interesting
things:
https://github.com/boostcon/cppnow_presentations_2013
----------------------
One is from "Concepts Lite: Constraining Templates with
Predicates" by Andrew Suttoon, Bjarne Stroustrup et al:
https://github.com/boostcon/cppnow_presentations_2013/blob/master/thu/concepts-lite.pdf?raw=true
This is a D version of C++13 code that uses Concepts Lite:
void sort(C)(C cont) if (SortableContainer!C) {}
They suggest to allow a syntax like this, usable when the
template constraint has only one argument:
void sort(SortableContainer cont) {}
It's handy especially for lambdas:
(Regular x) => x == y;
If you have two or more types, they must be the same (if you
don't want this, you have to use the normal longer syntax):
void foo(isRandomAccessRange a,
isRandomAccessRange b) {
static assert(is(typeof(a) == typeof(b))); // true.
}
The total number of concepts they define is quite small:
Equality_comparable
Totally_ordered
Regular
Function
Predicate
Relation
Input_iterator
Forward_iterator
Bidirectional_iterator
Sortable
----------------------
The other nice thing I've seen is here:
"Dynamic, Recursive, Heterogeneous Types in Statically-Typed
Languages" by Richard Saunders, Clinton Jeffery:
https://github.com/boostcon/cppnow_presentations_2013/blob/master/fri/DynRec.pdf?raw=true
They define Tab, Val and little else that allows them to offer a
dynamic data structure as similar as possible to the Python dicts:
# Python
d = {'a':1, 'b':2.2, 'c':[1,2.2,'three']}
v = int(d['c'][0])
v +=3
d['c'][2] = v
The C++ syntax they use:
Tab d="{'a':1,'b':2.2,'c':[1,2.2,'three']}";
int v = d("c")(0);
v += 3;
d["c"][2] = v;
Another example in C++:
Tab t = "{'a':{'nest':1}}";
cout << t["a"]["nest"] << endl;
t["a"]["nest"] = 17;
Bye,
bearophile
More information about the Digitalmars-d
mailing list