将函数替换为模板时,编译器由于某种原因看不到它。虽然他看到了上面的两条线。
#include <iostream>
#include <iomanip>
#include <string>
#include <endian.h>
using namespace std;
template <typename RESULT_T, typename letoh_func, typename T>
static RESULT_T compose_high_low(T high, T low)
{
RESULT_T res = letoh_func(high);
res <<= sizeof(T) * 8;
res |= letoh_func(low);
return res;
}
int main()
{
uint16_t k = le16toh(1);
uint16_t high = 1, low = 2;
uint32_t n = compose_high_low<uint32_t, le16toh>(high, low); // error: 'le16toh' was not declared in this scope
cout << hex << n << endl;
return 0;
}
在这里运行:http: //cpp.sh/7w7afl
le16toh
通常是一个宏。并且要将函数指针作为模板参数传递,您必须使用非类型参数。一般来说,使用boost::endian
.