【发布时间】:2017-01-07 06:02:11
【问题描述】:
问题:是否可以通过将(可能使用某种“完美转发”)其参数传递给内部 constexpr 函数来评估函数内部的常量表达式? 示例:
constexpr size_t foo(char const* string_literal) {
return /*some valid recursive black magic*/;
}
void bar(char const* string_literal) {
// works fine
constexpr auto a = foo("Definitely string literal.");
// compile error: "string_literal" is not a constant expression
constexpr auto b = foo(string_literal);
}
template<typename T>
void baz(T&& string_literal) {
// doesn't compile as well with the same error
constexpr auto b = foo(std::forward<T>(string_literal));
}
int main() {
// gonna do this, wont compile due to errors mentioned above
bar("Definitely string literal too!");
}
在documentation 中找不到任何明确禁止的内容,但没有找到解决方案,以及不可能的证明。内在表达的约束性很重要。
【问题讨论】:
标签: c++ c++11 constexpr perfect-forwarding