Home > Mobile >  Error: there are no arguments to 'static_assert' that depend on a template parameter
Error: there are no arguments to 'static_assert' that depend on a template parameter

Time:11-11

When compiling this code, I get an error:

#include <cstdio>

template<size_t Index, typename T, size_t Length>
T& get(T (&arr)[Length]) {
    static_assert(Index < Length, "Index out of bounds");
    return arr[Index];
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int value = get<5>(arr);
    printf("value = %d\n", value);
}

Error message:

In function 'T& get(T (&)[Length])':
Line 5: error: there are no arguments to 'static_assert' that depend on a template parameter, so
a declaration of 'static_assert' must be available
compilation terminated due to -Wfatal-errors.

Can someone explain how to fix this please?

Running with gcc 4.1.2
flags: -O -std=c 98 -pedantic-errors -Wfatal-errors -Werror -Wall -Wextra -Wno-missing-field-initializers -Wwrite-strings -Wno-deprecated -Wno-unused -Wno-non-virtual-dtor -Wno-variadic-macros -fmessage-length=0 -ftemplate-depth-128 -fno-merge-constants -fno-nonansi-builtins -fno-gnu-keywords -fno-elide-constructors -fstrict-aliasing -fstack-protector-all -Winvalid-pch

CodePudding user response:

This language feature became available in C only with C 11, see:

https://en.cppreference.com/w/cpp/language/static_assert

  • Related