Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #include <array>
  2. #include <iostream>
  3. #include <memory>
  4. #include <string>
  5. #include <type_traits>
  6. #include <utility>
  7.  
  8. #include <cxxabi.h>
  9.  
  10.  
  11. template <typename T>
  12. std::string type(const T& arg)
  13. {
  14. auto status {0};
  15.  
  16. std::unique_ptr<char, decltype(&std::free)> p {
  17. abi::__cxa_demangle(typeid(arg).name(), nullptr, nullptr, &status),
  18. [](void* p) noexcept { std::free(p); }
  19. };
  20.  
  21. return p.get();
  22. }
  23.  
  24.  
  25. template <typename C>
  26. class constexpr_string
  27. {
  28. public:
  29. using char_type = typename std::char_traits<C>::char_type;
  30. using size_type = typename std::basic_string<C>::size_type;
  31.  
  32. private:
  33. template <size_type... Ts>
  34. using ixseq = std::integer_sequence<size_type, Ts...>;
  35.  
  36. template <size_type N>
  37. using mkixseq = std::make_integer_sequence<size_type,N>;
  38.  
  39. // const char_type* data_;
  40.  
  41. public:
  42. // constexpr constexpr_string(const char_type* data = nullptr)
  43. // : data_ {data}
  44. // {}
  45. //
  46. // template <typename... Ts>
  47. // constexpr constexpr_string(Ts&&... args)
  48. // : data_ {cat(std::forward<Ts>(args)...)}
  49. // {}
  50.  
  51. ~constexpr_string() = default;
  52.  
  53. template <typename T, typename U>
  54. static constexpr auto cat(T&& lhs, U&& rhs = "") noexcept
  55. { return cat_(std::forward<T>(lhs), std::forward<U>(rhs), mkixseq<size<T>::value-1>(), mkixseq<size<U>::value>()); }
  56.  
  57. template <typename T, typename U, typename... Ts>
  58. static constexpr auto cat(T&& lhs, U&& rhs, Ts&&... args) noexcept
  59. { return cat(std::forward<T>(lhs), cat(std::forward<U>(rhs), std::forward<Ts>(args)...)); }
  60.  
  61. private:
  62. template <typename T, size_type N>
  63. static constexpr auto size_(const T(&)[N]) noexcept
  64. -> std::integral_constant<size_type,N>
  65. { return {}; }
  66.  
  67. template <typename T, size_type N>
  68. static constexpr auto size_(const std::array<T,N>&) noexcept
  69. -> std::integral_constant<size_type,N>
  70. { return {}; }
  71.  
  72. template <typename T>
  73. using size = decltype(size_(std::declval<T>()));
  74.  
  75. template <typename T, typename U, size_type... Ix1, size_type... Ix2>
  76. static constexpr auto cat_(const T& lhs, const U& rhs, ixseq<Ix1...>, ixseq<Ix2...>) noexcept
  77. -> std::array<char_type, size<T>::value + size<U>::value - 1>
  78. { return {lhs[Ix1]..., rhs[Ix2]...}; }
  79. };
  80.  
  81.  
  82. int main(int argc, char** argv)
  83. {
  84. constexpr auto hoge {constexpr_string<char>::cat("hell", "o w", "orld!")};
  85.  
  86. std::cout << type(hoge) << ": " << hoge.data() << std::endl;
  87.  
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement