template
class SomeSerializerClass
{
private:
static constexpr int m_version = 1;
virtual std::vector serialize_payload(const TPayload& payload) = 0;
public:
[[nodiscard]] std::vector serialize(const TPayload& payload)
{
std::vector res;
if constexpr (m_version == 1)
{
/// rest of the implementation
auto payload_serialized = serialize_payload(payload);
/// rest of the implementation
}
else
{
static_assert(std::false_type::value, "version not implemented");
}
return res;
}
public:
/// other stuff
};
/// a simple implementation
class Test : public SomeSerializerClass
{
private:
std::vector serialize_payload(const uint64_t& payload) override
{
return std::vector();
}
};
< /code>
Вот полная строка компиляции с флагами: < /p>
g++ -std=c++20 -Wall -Wextra -g -Iinclude -Iinclude/messages -c -MMD src/demo.cpp -o src/demo.o
< /code>
Я пробовал его с помощью ontinlegdb (c ++ 20), и там я думаю, что функция вообще не создана, потому что даже с Af contexpr (std :: false_type :: value )
Код будет компилироваться. Чего мне здесь не хватает?
Следующая часть кода попадает в блок Else, независимо от логики, если Constexpr и статическое утверждение не удается с G ++ 11.4.0: [code]template class SomeSerializerClass { private: static constexpr int m_version = 1;
if constexpr (m_version == 1) { /// rest of the implementation auto payload_serialized = serialize_payload(payload); /// rest of the implementation } else { static_assert(std::false_type::value, "version not implemented"); }
return res; } public: /// other stuff
};
/// a simple implementation class Test : public SomeSerializerClass { private: std::vector serialize_payload(const uint64_t& payload) override { return std::vector(); } }; < /code> Вот полная строка компиляции с флагами: < /p> g++ -std=c++20 -Wall -Wextra -g -Iinclude -Iinclude/messages -c -MMD src/demo.cpp -o src/demo.o < /code> Я пробовал его с помощью ontinlegdb (c ++ 20), и там я думаю, что функция вообще не создана, потому что даже с Af contexpr (std :: false_type :: value ) [/code] Код будет компилироваться. Чего мне здесь не хватает?