Ich versuche, eine Funktion zu schreiben, die eine Aufzählung von Werten auf eine Reihe von Typen basierend auf dem Laufzeitwert der Aufzählung abbildet. Ich weiß, dass Sie verschiedene Typen nicht basierend auf dem Laufzeitwert einer Enumeration zurückgeben können, da der Compiler nicht wissen, wie viel Stapelspeicherplatz zuzuordnen ist. Jedoch versuche ich dies als eine constexpr-Funktion zu schreiben, die die neue if-constexpr-Funktionalität verwendet, um dies zu implementieren.Können constexpr-if-else-Karosserien unterschiedliche Typen in der constexpr-Autofunktion zurückgeben?
Ich bekomme einen Fehler von clang beschweren, dass ich einen illegal angegebenen Template-Parameter verwende. Kann jemand sehen, wie man das umsetzt?
bearbeiten: Hier ist eine einfachere Version grok mein Problem prägnant mehr demonstriert: http://coliru.stacked-crooked.com/a/2b9fef340bd167a8
alten 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);
}
}
} // ns anon
////////////////////////////////////////////////////////////////////////////////////////////////////
// demo
int main()
{
}
Fehler:
/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)
Was ist mit all den '' '' std :: size_t'''' in Ihren Template-Parameter? – mascoj
das sieht nach einer überentwickelten Fabrikmethode aus. – Swift
Ihr Fehler hat nichts mit 'if conexpr' zu tun (nicht dass Sie es richtig benutzen). –