0

I am trying to extend the code from the answer to this question Unpack parameter pack into string view.

Instead of stringifying char-only types, I would like to use a constexpr_string type, custom written:

#include <array>
#include <iostream>

class constexpr_string {
public:
  template <std::size_t N>
  constexpr constexpr_string(const char (&s)[N]) : string_{s}, size_{N - 1} {}
  constexpr constexpr_string() = default;
  constexpr char const operator[](std::size_t n) const { return string_[n]; }
  constexpr std::size_t GetSize() { return size_; }

private:
  char const *string_{nullptr};
  std::size_t size_{0};
};

template <constexpr_string... strings> constexpr auto stringify() {
  std::array<constexpr_string, sizeof...(strings)> array = {strings...};
  return array;
}

int main() {
  static constexpr auto s = stringify<"abc", "def", "fgh">();

  for (auto &i : s) {
    std::cout << i << std::endl;
  }
  return 0;
}

When I compile though, I get :

main.cpp:18:31: error: ‘class constexpr_string’ is not a valid type for a template non-type parameter template constexpr auto stringify()

Is such a thing even possible? I'm compiling with g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0. Many thanks in advance!

Mihai Galos
  • 1,707
  • 1
  • 19
  • 38
  • FWIW, it will be possible to use an appropriate type as a NTTP in C++20 and thus use a string literal as a template argument. – chris Mar 22 '19 at 21:27
  • I do not think this is a duplicate, since the stringifying takes place at compile time and is independent of rtti. The answer to that question uses rtti to stringify at runtime, not compile time. /cc @πάντα ῥεῖ – Mihai Galos Mar 22 '19 at 21:30

0 Answers0