所以,假设我正在编写一个函数来使用每个项目的用户提供的回调来设置数组.(我不是,但我想我是,为了一个最小的例子)
我能找到的最干净的方法如下:
#include <functional>
template<typename T, typename Y>
void PopulateArray(std::function<int(Y*)> callback, T &pArray)
{
for (int i = 0; i < sizeof(pArray); ++i)
int x = callback(&pArray[i]);
}
int main()
{
uint64_t myArray[5];
uint64_t myUint = 42;
PopulateArray( (std::function<int(uint64_t*)>) [=](auto x) {*x = myUint; return 0; },
myArray);
}
Run Code Online (Sandbox Code Playgroud)
我上面的代码有两个问题.
1)对于T数组类型,似乎无法修改参数.(我不能说我想要一个类型的数组T,这意味着我必须Y单独声明,即使它们都是相关的uint64_t.)我更愿意声明一个T,一个参数是一个指针,T另一个是一个数组的T.
2)客户端代码(在main中)被强制转换lambda.更改auto x为显式类型似乎没有帮助.
是否有#1或#2的解决方案可能使代码更简洁或可读?
代码需要使用gcc,clang和VS进行编译.我认为 C++ 11是我可以使用的最新标准,虽然我对C++ 14解决方案感兴趣,因为这将是升级我们的clang构建过程的问题.我对涉及切换myArray std::array std::vector等的解决方案不感兴趣.
删除以下要求std::function:
// You could consider using an array type for the parameter:
// template <typename Callback, typename T, std::size_t N>
// void PopulateArray(Callback callback, T (&pArray)[N])
template<typename Callback, typename T>
void PopulateArray(Callback callback, T& pArray)
{
// sizeof(pArray) as in the question is almost certainly not what you
// want. It returns the size *in bytes*, not the length of the array.
// Thus, if you specified this to take an array reference,
// `for (std::size_t i = 0; i < N; ++i)` would be correct.
// However, as Barry mentioned in the comments, a range-based for loop
// is the best solution.
for (T& element : pArray)
callback(&element);
}
int main()
{
std::uint64_t myArray[5];
PopulateArray([](auto x) {*x = 42; return 0; },
myArray);
}
Run Code Online (Sandbox Code Playgroud)
std::function是一种昂贵的类型.它使用虚函数调用(或非常相似的技术)并具有分配内存的潜力.如果您没有存储该函数,特别是如果该函数已经是模板,则只需将任意回调作为参数.如果您真的想约束回调的类型,请使用function_ref类型(尚未标准化),或检查callback(your, args)是否有效:
template<typename Callback, typename T>
auto PopulateArray(Callback callback, T& pArray)
-> decltype(callback(*std::begin(pArray)), void())
{
for (T& element : pArray)
callback(&element);
}
Run Code Online (Sandbox Code Playgroud)
此外,在这种特定情况下,您可以使用算法:
int main()
{
uint64_t myArray[5];
uint64_t myUint = 42;
// If it's all the same value:
std::fill(std::begin(myArray), std::end(myArray), myUint);
// To call a function to populate the array:
std::generate(std::begin(myArray), std::end(myArray), [myUint] {
return myUint;
});
// Or possibly:
std::for_each(std::begin(myArray), std::end(myArray),
[myUint](uint64_t& element) {
element = myUint;
});
}
Run Code Online (Sandbox Code Playgroud)