Sunday 15 July 2012

c++ - How to avoid "conditional expression is constant" warning with compile-time-constant conditions in template code? -



c++ - How to avoid "conditional expression is constant" warning with compile-time-constant conditions in template code? -

consider code:

template <typename t> cbytearray serialize(const t& value) { if (std::is_pod<t>::value) homecoming serializepodtype(value); else if (std::is_convertible<t, variant>::value) homecoming serialize(variant(value)); else { assert(0 == "unsupported type"); homecoming cbytearray(); } }

obviously, compiler right give me warning if (std::is_pod<t>::value) etc., how circumvent this? can't find way avoid check, , there's no static if in c++ (yet).

can sfinae principle used avoid if?

can sfinae principle used avoid if?

yes, @ to the lowest degree non-default cases:

template <typename t> typename std::enable_if<std::is_pod<t>::value, cbytearray>::type serialize(const t& value) { homecoming serializepodtype(value); } template <typename t> typename std::enable_if< !std::is_pod<t>::value && // needed if pod types can converted variant std::is_convertible<t, variant>::value, cbytearray>::type serialize(const t& value) { homecoming serialize(variant(value)); }

if want run-time, rather compile-time, error unsupported types, declare variadic function grab arguments don't match other overloads.

cbytearray serialize(...) { hlassert_unconditional("unsupported type"); homecoming cbytearray(); }

c++ templates

No comments:

Post a Comment