请考虑以下代码:
static constexpr int make_const(const int i){
return i;
}
void t1(const int i)
{
constexpr int ii = make_const(i); // error occurs here (i is not a constant expression)
std::cout<<ii;
}
int main()
{
t1(12);
}
Run Code Online (Sandbox Code Playgroud)
为什么我在make_const调用时出错?
UPDATE
但是这个有效:
constexpr int t1(const int i)
{
return make_const(i);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不是:
template<int i>
constexpr bool do_something(){
return i;
}
constexpr int t1(const int i)
{
return do_something<make_const(i)>(); // error occurs here (i is not a constant expression)
}
Run Code Online (Sandbox Code Playgroud)