简体   繁体   English

错误:“static_assert”没有依赖于模板参数的参数

[英]Error: there are no arguments to 'static_assert' that depend on a template parameter

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使用 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标志: -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

This language feature became available in C++ only with C++11, see:此语言功能仅在 C++11 中才在 C++ 中可用,请参阅:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM