Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bevált C++ technikák 2007.12.10. Budapest. Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming.

Similar presentations


Presentation on theme: "Bevált C++ technikák 2007.12.10. Budapest. Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming."— Presentation transcript:

1 Bevált C++ technikák Budapest

2 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

3 Fabejárás template struct Tree { Tree* left; Tree* right; Data* data; }; template void preorder(Tree * tree) { if (tree->data) [process *tree->data] if (tree->left) preorder(tree->left); if (tree->right) preorder(tree->right); } 1. void printData(Data&); 2. class MyClass { // … void processData(Data&); } myClass; 3. void printData2(Data&, int param); 4. class MyClass { // … void processData2(Data&, int); } myClass;

4 Fabejárás, függvénypointer 1. void printData(Data&); 2. class MyClass { // … void processData(Data&); } myClass; 3. void printData(Data&, int param); 4. class MyClass { // … void processData(Data&, int); } myClass; template void preorder(Tree * tree, void (*f)(Data&)) { if (tree->data) (*f)(*tree->data); if (tree->left) preorder(tree->left, f); if (tree->right) preorder(tree->right, f); }

5 Fabejárás, STL funktor Hátrány: mindig új típust (osztályt) kell létrehozni egy operator()(Data&) művelettel template void preorder(Tree * tree, Functor f) { if (tree->data) f(*tree->data); if (tree->left) preorder(tree->left, f); if (tree->right) preorder(tree->right, f); } struct printDataFunctor { void operator()(Data& d) { printData(d); } };

6 Boost.bind Tree szavak; preorder(&szavak, boost::bind(&printData, _1)); preorder(&szavak, boost::bind(&MyClass::processData, &myClass, _1)); preorder(&szavak, boost::bind(&printData2, _1, 5)); preorder(&szavak, boost::bind(&MyClass::processData2, &myClass, _1, 5)); struct X { std::string name }; std::find_if(first, last, bind(&X::name, _1) == "peter"); std::sort(first, last, bind(&X::name, _1) < bind(&X::name, _2));

7 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

8 Static assert template class StaticAssert; template<> class StaticAssert {}; template class Array { public: char data[1 << size]; StaticAssert sizeConstraint; }; Array array8; // ok Array array10; // error

9 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

10 Boost.spirit, calculator group ::= '(' expression ')‘ factor ::= integer | group term ::= factor (('*' factor) | ('/' factor))* expression ::= term (('+' term) | ('-' term))* (-1 + 2) * (3 + -4) 1 + ((6 * 200) - 20) / 6 struct calculator : public grammar { template struct definition { definition(calculator const& self) { group = '(' >> expression >> ')'; factor = integer | group; term = factor >> *(('*' >> factor) | ('/' >> factor)); expression = term >> *(('+' >> term) | ('-' >> term)); } rule expression, term, factor, group; rule const& start() const { return expression; } };

11 Boost.spirit, actions void do_int(char const* str, char const* end); void do_add(char const*, char const*); void do_subt(char const*, char const*); void do_mult(char const*, char const*); void do_div(char const*, char const*); void do_neg(char const*, char const*); expression = term >> *( ('+' >> term)[&do_add] | ('-' >> term)[&do_subt] ) ; term = factor >> *( ('*' >> factor)[&do_mult] | ('/' >> factor)[&do_div] ) ; factor = lexeme_d[(+digit_p)[&do_int]] | '(' >> expression >> ')' | ('-' >> factor)[&do_neg] | ('+' >> factor) ; DSEL – Domain Specific Embedded Language

12 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

13 Template metaprogramming template class Factorial { public: enum { value = N*Factorial ::value }; }; template<> class Factorial { public: enum { value = 1 }; }; int main () { return Factorial ::value; }

14 Template metaprogramming (2) // accumulate(n,f) := f(0) + f(1) f(n) template class F> struct Accumulate { enum { RET=Accumulate ::RET+F ::RET }; }; template class F> struct Accumulate { enum { RET = F ::RET }; }; template struct Square { enum { RET = n*n }; }; int main() { return Accumulate ::RET; // 0^2 + 1^2 + 2^2 + 3^2 = 14 }

15 Type list struct Null {}; template struct TList { typedef H Head; typedef T Tail; }; typedef TList > > integral_types; template struct TListCount; template struct TListCount > { enum { Result = 1 + TListCount ::Result }; }; template<> struct TListCount { enum { Result = 0 }; }; const int n = TListCount ::Result; // 3

16 Generated class hierarchy template struct Hierarchy; template struct Hierarchy > : public H, public Hierarchy {}; template<> struct Hierarchy {}; Hierarchy > > > template Base> struct Hierarchy2; template Base> struct Hierarchy2, Base> : public Base, public Hierarchy {}; template Base> struct Hierarchy2 {}; A B C Base

17 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

18 Boost.preprocessor #define TLIST_OP(s,x,h)TList #define TLIST(t) \ BOOST_PP_SEQ_FOR_EACH(TLIST_OP, x, t) Null \ BOOST_PP_REPEAT(BOOST_PP_SEQ_SIZE(t), ENDBRACKET, data) typedef TLIST((short)(int)(long)) integrals;

19 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

20 Concepts template where Assignable void fill(Fwd first, Fwd last, const T& t); concept Assignable { Var a; Var b; a = b; }; assert Assignable >;

21 Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming Concepts Irodalom, contact

22 A.Alexandrescu: Modern C++ Design D.Abrahams, A.Gurtovoy: C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond Boost libraries: Specifying C++ concepts: std.org/JTC1/SC22/WG21/docs/papers/2005/n1886.pdf Mihalicza József


Download ppt "Bevált C++ technikák 2007.12.10. Budapest. Tartalom Funktor, boost.{bind, lambda} Static assert Boost.spirit Template metaprogramming Preprocessor metaprogramming."

Similar presentations


Ads by Google