Однако, если я использую функцию для фактической генерации целого числа из этих значений, msvc выдает ошибку компилятора:
Код: Выделить всё
error C3615: constexpr function 'Color::operator int' cannot result in a constant expression
note: failure was caused by call of undefined function or one not declared 'constexpr'
note: see usage of '::operator ()'
Код: Выделить всё
#include
#include
namespace detail
{
template
union binary_fusion_helper
{
const From from[Size];
const To to;
};
template
constexpr To binary_fusion(Arg arg, Args... args)
{
using in_t = std::remove_reference_t;
using out_t = To;
static_assert(sizeof(out_t) == sizeof(in_t) * (sizeof...(Args) + 1), "The target type must be of exact same size as the sum of all argument types.");
constexpr size_t num = sizeof(out_t) / sizeof(in_t);
return binary_fusion_helper { std::forward(arg), std::forward(args)... }.to;
}
}
template
constexpr auto binary_fusion = [](auto ...values) -> To
{
return detail::binary_fusion(values...);
};
struct Color
{
float r, g, b, a;
explicit constexpr operator int() const noexcept
{
return binary_fusion(static_cast(r * 255), static_cast(g * 255),
static_cast(b * 255), static_cast(a * 255));
}
};
Подробнее здесь: https://stackoverflow.com/questions/538 ... expression
Мобильная версия