我想知道是否有任何方法可以在本地使用typedef
与函数参数和函数体。
类似于以下情况:
// typedef: solution 1, visible everywhere in the namespace
void func(/*really long type*/ param1, /*many more params of same/different type*/) {
// typedef: solution 2, cannot use for params
/*more variables using the same typedef inside the body */
}
我知道我可以在函数体中有typedef
,但这对参数不可用。我想让它更可读,特别是考虑到长类型名称和同一类型的多个参数。函数外的typedef
会污染命名空间,除非别无选择,否则我想防止这种情况发生。
有没有一种我不知道的方法可以在更本地化的同时实现上述目标?如果没有,建议使用哪种解决方案。
您可以执行以下操作:
namespace inner {
using type = really_long_type; // typedef works as well
void func(type t, /* ... */) {
// ...
}
}
using inner::func;