I am trying to write a function that maps an enumeration of values to a set of types based on the execution time of an enumeration. I understand that you cannot return different types depending on the execution time of the enumeration, because the compiler does not know how much stack space is allocated. However, I am trying to write this as a constexpr function, using the new if-constexpr functionality to implement it.
I get a message from clang complaining that I am using the illegally specified template parameter. Does anyone see how to implement this?
edit: Here is a simpler version of grok showing my problem more briefly:
http://coliru.stacked-crooked.com/a/2b9fef340bd167a8
old code:
#include <cassert>
#include <tuple>
#include <type_traits>
namespace
{
enum class shape_type : std::size_t
{
TRIANGLE = 0u,
RECTANGLE,
POLYGON,
CUBE,
INVALID_SHAPE_TYPE
};
template<std::size_t T>
struct shape {
};
using triangle = shape<static_cast<std::size_t>(shape_type::TRIANGLE)>;
using rectangle = shape<static_cast<std::size_t>(shape_type::RECTANGLE)>;
using polygon = shape<static_cast<std::size_t>(shape_type::POLYGON)>;
using cube = shape<static_cast<std::size_t>(shape_type::CUBE)>;
template<std::size_t A, std::size_t B>
static bool constexpr same() noexcept { return A == B; }
template<std::size_t ST>
static auto constexpr make_impl(draw_mode const dm)
{
if constexpr (same<ST, shape_type::TRIANGLE>()) {
return triangle{};
} else if (same<ST, shape_type::RECTANGLE>()) {
return rectangle{};
} else if (same<ST, shape_type::POLYGON>()) {
return polygon{};
} else if (same<ST, shape_type::CUBE>()) {
return cube{};
} else {
assert(0 == 5);
}
}
static auto constexpr make(shape_type const st, draw_mode const dm)
{
switch (st) {
case shape_type::TRIANGLE:
return make_impl<shape_type::TRIANGLE>(dm);
case shape_type::RECTANGLE:
return make_impl<shape_type::RECTANGLE>(dm);
case shape_type::POLYGON:
return make_impl<shape_type::POLYGON>(dm);
case shape_type::CUBE:
return make_impl<shape_type::CUBE>(dm);
case shape_type::INVALID_SHAPE_TYPE:
assert(0 == 17);
}
}
}
int main()
{
}
Errors:
/home/benjamin/github/BoomHS/main.cxx:42:6: warning: constexpr if is a
C++1z extension [-Wc++1z-extensions]
if constexpr (same<ST, shape_type::TRIANGLE>()) {
^ /home/benjamin/github/BoomHS/main.cxx:59:16: error: no matching function for call to 'make_impl'
return make_impl<shape_type::TRIANGLE>(dm);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/benjamin/github/BoomHS/main.cxx:40:23: note: candidate template
ignored: invalid explicitly-specified argument for template parameter
'ST' static auto constexpr make_impl(draw_mode const dm)
^ /home/benjamin/github/BoomHS/main.cxx:61:16: error: no matching function for call to 'make_impl'
return make_impl<shape_type::RECTANGLE>(dm);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/benjamin/github/BoomHS/main.cxx:40:23: note: candidate template
ignored: invalid explicitly-specified argument for template parameter
'ST' static auto constexpr make_impl(draw_mode const dm)
^ /home/benjamin/github/BoomHS/main.cxx:63:16: error: no matching function for call to 'make_impl'
return make_impl<shape_type::POLYGON>(dm);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/benjamin/github/BoomHS/main.cxx:40:23: note: candidate template
ignored: invalid explicitly-specified argument for template parameter
'ST' static auto constexpr make_impl(draw_mode const dm)
^ /home/benjamin/github/BoomHS/main.cxx:65:16: error: no matching function for call to 'make_impl'
return make_impl<shape_type::CUBE>(dm);
^~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/benjamin/github/BoomHS/main.cxx:40:23: note: candidate template
ignored: invalid explicitly-specified argument for template parameter
'ST' static auto constexpr make_impl(draw_mode const dm)
Short source
share