【发布时间】:2013-09-19 01:40:19
【问题描述】:
我有一个名为 access 的 constexpr 函数,我想访问数组中的一个元素:
char const*const foo="foo";
char const*const bar[10]={"bar"};
constexpr int access(char const* c) { return (foo == c); } // this is working
constexpr int access(char const* c) { return (bar[0] == c); } // this isn't
int access(char const* c) { return (bar[0] == c); } // this is also working
我得到了错误:
error: the value of 'al' is not usable in a constant expression
为什么我不能从 access 中访问其中一个元素?或者如果可能的话,我该怎么做?
【问题讨论】:
-
有效吗?还是不行?
-
@MikeSeymour 好的,我不懂 C++ 我应该删除评论吗?
-
@GrijeshChauhan 对不起,不,它不起作用。我的方法在没有
constexpr的情况下有效。