除了使代码更清晰之外,在 C++ 中,它在编译时强制函数仅适用于一组可能值中的一个:
namespace Foo
{
enum Bar { A, B, C };
void foo(Bar b) { .... }
void foo2(int i) { /* only ints between 0 and 5 make sense */ }
}
int main()
{
Foo::Bar b = Foo::A;
Foo::foo(b); // OK
Foo::foo(245); // Compile-time error!
Foo::foo2(6); // Compiles, triggering some run-time error or UB
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12890 次 |
| 最近记录: |