假设我想编写一个执行整数平方根的C++ 1y/14 constexpr函数:
constexpr int constexpr_isqrt(int x);
Run Code Online (Sandbox Code Playgroud)
我想进行一次完整性检查以确保它x是非负面的:
constexpr int constexpr_isqrt(int x)
{
if (x < 0)
???
...
}
Run Code Online (Sandbox Code Playgroud)
我应该在???上面写什么?
理想情况下,如果在常量上下文中计算函数,它应该导致编译时错误,并且如果在运行时调用运行时错误(例如中止或抛出异常).
ste*_*fan 11
你很幸运,有办法!即使在C++ 11中!使用例外:
#include <iostream>
#include <stdexcept>
constexpr int foo(int a)
{
return (a >= 0) ? a : throw std::invalid_argument("Negative!");
}
template <int n>
struct Foo
{
};
int main()
{
Foo<foo(1)> f1(); // fine, guaranteed compile time
Foo<foo(-1)> f2(); // bad, compile time error
foo(1); // fine, not necessarily at compile time
try
{
foo(-1); // fine, definitively not at compile time
}
catch ( ... )
{
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC对于不允许的案例有一个相当不错的错误信息:
prog.cpp: In function ‘int main()’:
prog.cpp:17:12: in constexpr expansion of ‘foo(-1)’
prog.cpp:6:63: error: expression ‘<throw-expression>’ is not a constant-expression
return (a >= 0) ? a : throw std::invalid_argument("Negative!");
Run Code Online (Sandbox Code Playgroud)
对于C++ 1y constexpr函数,看起来像
constexpr foo(int a)
{
if ( a < 0 )
{
// error!
}
++a;
//something else
return a;
}
Run Code Online (Sandbox Code Playgroud)
你可以通过引入一个新函数来使用上面的模式:
constexpr foo_positive(int a)
{
++a;
//something else
return a;
}
constexpr int foo(int a)
{
return (a >= 0) ? foo_positive(a) : throw std::invalid_argument("Negative!");
}
Run Code Online (Sandbox Code Playgroud)
或者你只是写
constexpr foo(int a)
{
if ( a < 0 )
{
throw std::invalid_argument("Negative!");
}
++a;
//something else
return a;
}
Run Code Online (Sandbox Code Playgroud)