【发布时间】:2018-12-29 14:27:19
【问题描述】:
我有两个 sn-ps。
第一个sn-p:
#include <string>
template <typename T>
constexpr bool foo(T&&) {
return false;
}
int main() {
std::string a;
if constexpr (foo(a)) {
}
}
第二个sn-p:
#include <string>
template <typename T>
constexpr bool foo(T&&) {
return false;
}
int main() {
std::string a;
std::string& x = a;
if constexpr (foo(x)) {
}
}
第一个编译,第二个不编译(错误信息:错误:'x'的值在常量表达式中不可用。为什么?为什么a可以使用在常量表达式中,x 不是?
该命令,用于编译g++ -std=c++17 main.cpp。
【问题讨论】:
标签: c++ c++17 constexpr compile-time constant-expression