【发布时间】:2018-11-10 10:40:00
【问题描述】:
我已经编写了一个函数来在 uint8_t 类型数组中引入随机字节,除非我使用 random_device 对象作为种子它工作正常,但是当我种子时会出现一些编译器错误。
代码:
#include <algorithm>
#include <random>
#include <functional>
#include <stdint>
void generateInitVector(uint8_t IV_buff[16])
{
using bytes_randomizer = std::independent_bits_engine<std::default_random_engine, CHAR_BIT, uint8_t>;
std::random_device rd;
bytes_randomizer bytes(rd);
std::generate(std::begin(IV_buff), std::end(IV_buff), std::ref(bytes));
}
编译器错误:
1. error: no matching function for call to 'begin(uint8_t*&)'|
2. error: request for member 'begin' in '__cont', which is of non-class type 'unsigned char*'|
3. error: request for member 'begin' in '__cont', which is of non-class type 'unsigned char* const'|
4. error: no matching function for call to 'end(uint8_t*&)'|
5. error: request for member 'begin' in '__cont', which is of non-class type 'unsigned char*'|
6. error: request for member 'end' in '__cont', which is of non-class type 'unsigned char* const'|
7. error: 'class std::random_device' has no member named 'generate'|
如果我在函数中定义 uint8_t 类型数组,就会发现
uint8_t data[16];
std::generate(std::begin(data), std::end(data), std::ref(bytes)); //and pass this array in `generate()`.
然后只剩下错误 7。有什么解决办法吗?
【问题讨论】:
-
使用
std::array。 -
@SombreroChicken 你能详细说明一下吗?
-
@UnholySheep 你能演示我如何实现你重定向我的解决方案吗?
-
无论如何你都不能在这里使用
uint8_t。仅short、int、long、long long或它们的未签名对应项,视情况而定,被允许作为<random>中的模板参数。甚至int64_t之类的也可能是 UB,尽管这纯粹是理论上的问题。
标签: c++ arrays c++11 random function-parameter