Check array contains element at compile time 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 &lt;array&gt;

#define COEFF(name) returnName&lt;coefficientExists(name)&gt;(name)

constexpr std::array&lt;const char*, 3&gt; COEFFICIENTS = { "VERSION", "CHANNELS", "ADAPTIVE" };

constexpr bool coefficientExists(const char* name)
{
    for (auto coefficientIndex = 0U; coefficientIndex &lt; COEFFICIENTS.size(); ++coefficientIndex)
    {
        if (COEFFICIENTS[coefficientIndex] == name)
            return true;
    }
    return false;
}

template&lt;bool CoefficientTest&gt;
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;
}


https://godbolt.org/z/kpGcMS
You have to compare values char by char, or even simpler change const char* to std::string_view make your code work.

Thanks to the deduction guides, you can just write std::array = ... and template parameters will be automatically deduced.

Also, you can simplify your loop using a "range-based for".
 If we generalize for other types of compile-time array, we get:

#include &lt;array&gt;
#include &lt;string_view&gt;

using namespace std::string_view_literals;
constexpr std::array COEFFICIENTS = { "VERSION"sv, "CHANNELS"sv, "ADAPTIVE"sv };

template &lt;typename T, size_t N&gt;
constexpr bool array_has(const std::array&lt;T, N&gt;&amp; array, const T&amp; value) {
  for (const auto&amp; v : array) {
    if (value == v) return true;
  }
  return false;
}

constexpr bool coefficientExists(const std::string_view name)
{
    return array_has(COEFFICIENTS, name);
}

int main()
{
    static_assert(coefficientExists("VERSION"), "should exist");
    static_assert(!coefficientExists("TEST"), "should not exist");
}


With C++20 you can even do simpler:

#include &lt;algorithm&gt;
//...
template &lt;typename T, size_t N&gt;
constexpr bool array_has(const std::array&lt;T, N&gt; array, const T value) {
return std::any_of(array.begin(), array.end(), [value](auto current){ return current == value; });
}