We have a modifyCoefficient(const char* name, int value)
function that updates the value of a coefficient in a container. The names are known at compile time, they are read from an XML file in a pre-build step and stored in an array.
Usage: data.modifyCoefficient("ADAPTIVE", 1);
Compiler: MSVC 2017 v15.8
I would like to get a compile time error when the coefficient name does not exist.
With the following code that happens, but is there a way to do it without a macro?
#include <array>
#define COEFF(name) returnName<coefficientExists(name)>(name)
constexpr std::array<const char*, 3> COEFFICIENTS = { "VERSION", "CHANNELS", "ADAPTIVE" };
constexpr bool coefficientExists(const char* name)
{
for (auto coefficientIndex = 0U; coefficientIndex < COEFFICIENTS.size(); ++coefficientIndex)
{
if (COEFFICIENTS[coefficientIndex] == name)
return true;
}
return false;
}
template<bool CoefficientTest>
constexpr const char* returnName(const char* name)
{
static_assert(CoefficientTest, "coefficient does not exist");
return name;
}
int main()
{
static_assert(coefficientExists("VERSION"), "should exist");
static_assert(coefficientExists("TEST") == false, "should not exist");
static_assert(COEFF("ADAPTIVE") == "ADAPTIVE", "should return name");
COEFF("CHANNELS");
// data.modifyCoefficient(COEFF("ADAPTIVE"), 1);
return 0;
}