Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash -vex
- WARN="-W -Wall -Wextra -Werror -Wsign-promo -Wconversion -Wsign-conversion"
- g++ -x c++ - -std=gnu++1y $WARN -Ofast -o a <<__EOF && ./a && echo -e "\e[1;32msucceeded\e[0m" || echo -e "\e[1;31mfailed\e[0m"
- #include <iostream>
- #include <iterator>
- #include <algorithm>
- #include <new>
- #include <array>
- #include <tuple>
- #include <cstdlib>
- #include <cassert>
- template< typename F >
- struct repacker
- {
- static_assert(std::is_pod< F >::value, "Underlying type is not a POD type.");
- using pointer = F *;
- explicit
- repacker(pointer const & _storage)
- : storage_(_storage)
- {
- assert((reinterpret_cast< std::uintptr_t >(storage_) % std::alignment_of< F >::value) == 0);
- }
- template< typename... P >
- constexpr
- auto operator () (P &&... p) const
- {
- constexpr auto const N = sizeof...(P);
- using A = F [N];
- static_assert((sizeof(A) == sizeof(F) * N), "Check an alignment of the underlying type and the packing of it.");
- std::ignore = new (storage_) A{static_cast< F >(p)...};
- return N;
- }
- private :
- pointer const & storage_;
- };
- int main()
- {
- using F = double;
- constexpr auto N = 6;
- F * a = new F[N];
- {
- F x(1.0);
- F const y(2.0);
- repacker< F > r(a);
- auto const M = r(x, y, 3.0, 4, 5.0f, 6.0L);
- assert(M == N);
- }
- std::copy_n(a, N, std::ostream_iterator< F const & >(std::cout, " "));
- std::cout << std::endl;
- delete [] a;
- return EXIT_SUCCESS;
- }
- __EOF
Advertisement
Add Comment
Please, Sign In to add comment