Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <iostream>
- #include <memory>
- #include <string>
- #include <type_traits>
- #include <utility>
- #include <cxxabi.h>
- template <typename T>
- std::string type(const T& arg)
- {
- auto status {0};
- std::unique_ptr<char, decltype(&std::free)> p {
- abi::__cxa_demangle(typeid(arg).name(), nullptr, nullptr, &status),
- [](void* p) noexcept { std::free(p); }
- };
- return p.get();
- }
- template <typename C>
- class constexpr_string
- {
- public:
- using char_type = typename std::char_traits<C>::char_type;
- using size_type = typename std::basic_string<C>::size_type;
- private:
- template <size_type... Ts>
- using ixseq = std::integer_sequence<size_type, Ts...>;
- template <size_type N>
- using mkixseq = std::make_integer_sequence<size_type,N>;
- // const char_type* data_;
- public:
- // constexpr constexpr_string(const char_type* data = nullptr)
- // : data_ {data}
- // {}
- //
- // template <typename... Ts>
- // constexpr constexpr_string(Ts&&... args)
- // : data_ {cat(std::forward<Ts>(args)...)}
- // {}
- ~constexpr_string() = default;
- template <typename T, typename U>
- static constexpr auto cat(T&& lhs, U&& rhs = "") noexcept
- { return cat_(std::forward<T>(lhs), std::forward<U>(rhs), mkixseq<size<T>::value-1>(), mkixseq<size<U>::value>()); }
- template <typename T, typename U, typename... Ts>
- static constexpr auto cat(T&& lhs, U&& rhs, Ts&&... args) noexcept
- { return cat(std::forward<T>(lhs), cat(std::forward<U>(rhs), std::forward<Ts>(args)...)); }
- private:
- template <typename T, size_type N>
- static constexpr auto size_(const T(&)[N]) noexcept
- -> std::integral_constant<size_type,N>
- { return {}; }
- template <typename T, size_type N>
- static constexpr auto size_(const std::array<T,N>&) noexcept
- -> std::integral_constant<size_type,N>
- { return {}; }
- template <typename T>
- using size = decltype(size_(std::declval<T>()));
- template <typename T, typename U, size_type... Ix1, size_type... Ix2>
- static constexpr auto cat_(const T& lhs, const U& rhs, ixseq<Ix1...>, ixseq<Ix2...>) noexcept
- -> std::array<char_type, size<T>::value + size<U>::value - 1>
- { return {lhs[Ix1]..., rhs[Ix2]...}; }
- };
- int main(int argc, char** argv)
- {
- constexpr auto hoge {constexpr_string<char>::cat("hell", "o w", "orld!")};
- std::cout << type(hoge) << ": " << hoge.data() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement