問題:是否有可能通過將參數傳遞給內部constexpr函數(可能帶有某種「完美轉發」)來評估函數內部的常量表達式? 例子:有沒有辦法將參數轉發到內部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,但解決的辦法是找不到的,以及不可能性的證明。內在表達的共同性很重要。
https://stackoverflow.com/questions/26582875/constexpr-function-parameters-as-template-arguments – SomeWittyUsername