I'm having trouble expanding an array reference. When I pass the array reference to a function template as an argument, the constant nature of the array reference seems to be lost:
#include <cstdlib>
#include <utility>
template<const char ... C>
struct hint_for_opt_cls_holder {
constexpr static const char value[] { '?', C... };
};
template<size_t N, size_t... Is>
static constexpr auto
create_hint_for_opt_cls(const char (&cname)[N],
std::index_sequence<Is...>) {
// fails: cname is not a constant expression
return hint_for_opt_cls_holder<cname[Is]...>::value;
}
template<size_t N>
static constexpr auto
create_hint_for_opt_cls(const char (&cname)[N]) {
constexpr decltype(auto) cname_ = cname; // fails already here, actually
return create_hint_for_opt_cls(cname,
std::make_index_sequence<N>());
}
constexpr static char name[] = "foobar";
constexpr static auto& get_class_name() {
return name;
}
int main() {
// works! knows the return is constant
constexpr decltype(auto) cname = get_class_name();
auto x = create_hint_for_opt_cls(cname);
}