compile time type transformation
play

Compile-time type transformation Meeting C++ 2019, Berlin dr Ivan - PowerPoint PPT Presentation

Compile-time type transformation Meeting C++ 2019, Berlin dr Ivan uki KDAB ivan.cukic@kdab.com, ivan@cukic.co https://kdab.com, https://cukic.co Slow introduction Compile-time Meta information Generation The End About me KDAB senior


  1. Compile-time type transformation Meeting C++ 2019, Berlin dr Ivan Čukić KDAB ivan.cukic@kdab.com, ivan@cukic.co https://kdab.com, https://cukic.co

  2. Slow introduction Compile-time Meta information Generation The End About me KDAB senior software engineer Software Experts in Qt, C++ and 3D / OpenGL Trainer / consultant KDE developer Author of the ”Functional Programming in C++” book University lecturer 2 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  3. Slow introduction Compile-time Meta information Generation The End Disclaimer Make your code readable. Pretend the next person who looks at your code is a psychopath and they know where you live. Philip Wadler 3 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  4. SLOW INTRODUCTION

  5. Slow introduction Compile-time Meta information Generation The End String concatenation std::string statement{”I'm not”}; std::string number{”a number”}; std::string space{” ”}; std::string period{”. ”}; std::string result = statement + space + number + period; 5 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  6. Slow introduction Compile-time Meta information Generation The End String concatenation 6 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  7. Slow introduction Compile-time Meta information Generation The End String concatenation std::string result; result.reserve(statement.size() + space.size() + number.size() + period.size()); result. append (statement); result. append (space); result. append (number); result. append (period); 7 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  8. Slow introduction Compile-time Meta information Generation The End String concatenation 8 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  9. Slow introduction Compile-time Meta information Generation The End String concatenation std::string concatenate(const std::vector<std::string>& strs) { std::string result; result.reserve(accumulate( strs | transform(&std::string:: size ), 0)); for (const auto& str: strs) { ⋯ } return result; } 9 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  10. Slow introduction Compile-time Meta information Generation The End String concatenation 10 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  11. Slow introduction Compile-time Meta information Generation The End String concatenation std::string concatenate(const std::vector<std::string>& strs) { std::string result; result.reserve(accumulate( strs | transform(&std::string:: size ), 0)); for (const auto& str: strs) { result.append(str); } return result; } 11 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  12. Slow introduction Compile-time Meta information Generation The End String concatenation std::string concatenate(const std::vector<std::string>& strs) { std::string result; result. resize (accumulate( strs | transform(&std::string::size), 0)); ??? return result; } 12 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  13. Slow introduction Compile-time Meta information Generation The End String concatenation std::string concatenate(const std::vector<std::string>& strs) { std::string result; result.resize(accumulate( strs | transform(&std::string::size), 0)); accumulate ( strs , result.begin() , [] (const auto& dest, const std::string& s) { return copy ( s , dest ); }); return result; } 13 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  14. Slow introduction Compile-time Meta information Generation The End String concatenation 14 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  15. Slow introduction Compile-time Meta information Generation The End Accumulate 15 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  16. Slow introduction Compile-time Meta information Generation The End Accumulate 16 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  17. Slow introduction Compile-time Meta information Generation The End Accumulate What if we want to save all the intermediate iterators into a vector? 17 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  18. Slow introduction Compile-time Meta information Generation The End Accumulate 18 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  19. Slow introduction Compile-time Meta information Generation The End Accumulate Accumulator type: struct acc_t { std::string:: iterator dest ; std:: vector <std::string:: iterator > points ; }; 19 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  20. Slow introduction Compile-time Meta information Generation The End Accumulate Initial accumulator value: acc_t { result.begin() , {} }; 20 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  21. Slow introduction Compile-time Meta information Generation The End Accumulate Accumulation function: [] (acc_t&& acc, const std::string& s) { acc. points . push_back (acc. dest ); return acc_t { copy ( s , acc. dest ), std::move(acc. points ) }; } 21 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  22. Slow introduction Compile-time Meta information Generation The End Accumulate std::string concatenate(const std::vector<std::string>& strs) { ⋯ struct acc_t { std::string::iterator dest; std::vector<std::string::iterator> points; }; auto acc = accumulate(strs, acc_t { result.begin(), {} }, [] (acc_t&& acc, const std::string& s) { acc.points.push_back(acc.dest); return acc_t { copy(s, acc.dest), std::move(acc.points) }; }); acc.dest ⋯ acc.points ⋯ } 22 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  23. Slow introduction Compile-time Meta information Generation The End Accumulate std::pair<std:: string , std:: vector <std:: string_view >> concatenate(const std::vector<std::string>& strs) { ⋯ } Note: Beware of string_view and UB 23 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  24. COMPILE-TIME

  25. Slow introduction Compile-time Meta information Generation The End Compile-time template < typename... Strings > auto concatenate(Strings&&... strs) { result.resize( accumulate (⋯)); } 25 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  26. Slow introduction Compile-time Meta information Generation The End Compile-time What is a compile-time accumulate? 26 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  27. Slow introduction Compile-time Meta information Generation The End Compile-time 27 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  28. Slow introduction Compile-time Meta information Generation The End Compile-time 28 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  29. Slow introduction Compile-time Meta information Generation The End Compile-time template < typename... Strings > auto concatenate(Strings&&... strs) { const auto total_size = ( 0 + ... + strs . size ()); std::string result; result.resize(total_size); ⋯ } 29 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  30. Slow introduction Compile-time Meta information Generation The End Compile-time template < typename... Strings > auto concatenate(Strings&&... strs) { const auto total_size = (0 + ... + strs.size()); std::string result; result.resize(total_size); (result.begin() << ... << strs); } 30 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  31. Slow introduction Compile-time Meta information Generation The End Compile-time template <typename Dest, typename String> auto operator<< (Dest dest, const String& string) { return copy(string, dest); } 31 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  32. Slow introduction Compile-time Meta information Generation The End Compile-time template <typename Init, typename Fun> struct accumulator_t { Init value ; const Fun& f ; accumulator_t(Init value, const Fun& f) : value{value}, f{f} {} ⋯ }; 32 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  33. Slow introduction Compile-time Meta information Generation The End Compile-time template <typename Init, typename Fun> struct accumulator_t { ⋯ template <typename T> auto operator<< (T&& t) const { return accumulator_t( std::invoke(f, value, std::forward<T>(t)), f); } ⋯ }; 33 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  34. Slow introduction Compile-time Meta information Generation The End Compile-time template <typename Init, typename Fun> struct accumulator_t { ⋯ operator Init() const { return value; } }; 34 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  35. Slow introduction Compile-time Meta information Generation The End Compile-time (accumulator_t( 0 , std:: plus {}) << ... << strs . size ()); 35 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

  36. Slow introduction Compile-time Meta information Generation The End Compile-time auto add_size = [](int previous, const std::string &s) { return previous + s.size(); }; (accumulator_t( 0 , add_size ) << ... << strs ); 36 Meeting C++ 2019, Berlin Ivan Č uki ć @ivan_cukic

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend