Advertisement
Dukales

aggregate adaptor

Nov 12th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <utility>
  2.  
  3. template< typename aggregate >
  4. struct embrace
  5.     : aggregate
  6. {
  7.    
  8.     template< typename ...arguments, bool is_noexcept = noexcept(aggregate{std::declval< arguments >()...}) >
  9.     embrace(arguments &&... _arguments) noexcept(is_noexcept)
  10.         : aggregate{std::forward< arguments >(_arguments)...}
  11.     { ; }
  12.    
  13. };
  14.  
  15. // main.cpp
  16. #include <type_traits>
  17.  
  18. #include <cstdlib>
  19. #include <cassert>
  20.  
  21. int
  22. main()
  23. {
  24.     struct A {};
  25.     struct B {};
  26.     {
  27.         struct S { A a; B b; }; // aggregate
  28.         using E = embrace< S >;
  29.         E e(A{}, B{}); // "parentheses"-constructible => can be used as usual types
  30.         e.a = {}; e.b = {}; // accessible
  31.         static_assert(std::is_constructible< E, A, B >{});
  32.         static_assert(std::is_constructible< E, A >{}); // "missed field initializers" warning desired anywere
  33.         static_assert(!std::is_constructible< E, struct C >{}); // don't want hard error here
  34.     }
  35.     {
  36.         struct S
  37.         {
  38.             A a;
  39.             B f() { return {}; }
  40.             B b = f();
  41.         };
  42.         using E = embrace< S >;
  43.         E e(A{}, B{}); // "parentheses"-constructible => can be used as usual types
  44.         e.a = {}; e.b = {}; // accessible
  45.         static_assert(std::is_constructible< E, A, B >{});
  46.         static_assert(std::is_constructible< E, A >{});
  47.         static_assert(!std::is_constructible< E, struct C >{}); // don't want hard error here
  48.     }
  49.     return EXIT_SUCCESS;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement