【发布时间】:2016-12-14 16:05:27
【问题描述】:
在一个接受多个相同类型参数的函数中,我们如何保证调用者不会弄乱顺序?
例如
void allocate_things(int num_buffers, int pages_per_buffer, int default_value ...
以后
// uhmm.. lets see which was which uhh..
allocate_things(40,22,80,...
【问题讨论】:
-
编译器可以在大多数情况下为您提供帮助。否则,这是你(程序员)的责任。
-
在 C++ 中使用特定类型不是很简单吗?
-
你能用method chaining吗?类似
allocate_thing().buffers(40).pages_per_buffer(22).default_value(80) -
这是个好问题。我认为唯一真正的解决方案是为需要配置的每个项目创建值类型。就像
<chrono>库使用 durations 例如std::chrono::seconds来配置时间段。 -
@gnasher - 同意,这是一个危险的功能 - 这使它成为一个特别好的例子。