当前位置:   article > 正文

C++20就要来了,综合模块和概念正式特性的一个例子_c++20模块例子

c++20模块例子

 

  1. // tooling.cpp
  2. export module cxx.fuck.u; // Exposed to consumers.
  3. import :details; // Consumers cannot import this module, internal only.
  4. export namespace tooling
  5. {
  6. class axxhole
  7. {
  8. public:
  9. axxhole() = default;
  10. inline operator std::string() { return "pretty"; }
  11. void feed()
  12. {
  13. details::feed_shit_impl(*this);
  14. }
  15. };
  16. }
  17. // tooling_details.cpp
  18. module cxx.fuck.u:details; // A module partition, for internal use only.
  19. import std.core;
  20. export namespace tooling::details
  21. {
  22. template<typename T>
  23. concept CastableToString = requires(T t)
  24. {
  25. { t }->std::string;
  26. };
  27. template<typename TTarget>
  28. void feed_shit_impl(TTarget&& target) requires CastableToString<TTarget>
  29. {
  30. auto name = static_cast<std::string>(std::forward<TTarget>(target));
  31. std::cout << "Feeding " << name << "... Delicious!" << std::endl;
  32. }
  33. }
  34. // Consumers
  35. import cxx.fuck.u;
  36. int main()
  37. {
  38. tooling::axxhole payload;
  39. payload.feed();
  40. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/208389?site
推荐阅读
相关标签
  

闽ICP备14008679号