c++ - using result of constexpr function as a template parameter (clang vs gcc) -
please take @ code below, sorry bit lengthy, did best reproduce problem minimum example (there live copy of it). there have metafunction returns size of string literal, , constexpr function wraps it. when call functions in template parameter gcc (5.4, 6.2) happy it, clang (3.8, 3.9) barfs "non-type template argument not constant expression" in test body on strsize(s)
. if replace str_size<s>
both compilers happy. questions are:
whether problem clang, or code?
what way make compile on both clang , gcc constexpr function?
template<size_t n> using string_literal_t = char[n]; template<class t> struct strsize; ///< metafunction size of string literal alikes /// specialize strsize string literals template<size_t n> struct strsize <string_literal_t<n>>{ static constexpr size_t value = n-1; }; /// template variable, convenience template <class t> constexpr size_t str_size = strsize<t>::value; /// same constexpr function template<class t> constexpr auto strsize(const t&) noexcept-> decltype(str_size<t>) { return str_size<t>; } template<class s, size_t... is> constexpr auto test_helper(const s& s, index_sequence<is...>) noexcept-> array<char, str_size<s>> { return {s[is]...}; } template<class s> constexpr auto test(const s& s) noexcept-> decltype(auto) { // return test_helper(s, make_index_sequence<str_size<s>>{}); // work in both clang , gcc return test_helper(s, make_index_sequence<strsize(s)>{}); // works in gcc } auto main(int argc, char *argv[])-> int { static_assert(strsize("qwe") == 3, ""); static_assert(noexcept(test("qwe")) == true, ""); return 0; }
Comments
Post a Comment