C++: Компилятор не подставляет функцию в шаблон
При подстановке функции в шаблон, компилятор почему-то её не видит. Хотя двумя строчками выше видит.
#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
Ответы (1 шт):
Автор решения: user7860670
→ Ссылка
le16toh часто является макросом. А чтобы передать в качестве параметра шаблона указатель на функцию, необходимо использовать параметр не-тип. И вообще, используйте boost::endian.
#include <boost/endian/conversion.hpp>
#include <ios>
#include <iostream>
#include <cstdint>
template <typename x_Result, typename x_Value>
static x_Result compose_high_low(x_Value const high, x_Value const low) noexcept
{
x_Result res{::boost::endian::little_to_native(high)};
res <<= sizeof(x_Value) * CHAR_BIT;
res |= ::boost::endian::little_to_native(low);
return res;
}
int main()
{
::std::cout << ::std::hex
<< compose_high_low<::std::uint32_t>(::std::uint16_t{1}, ::std::uint16_t{2})
<< ::std::endl;
return 0;
}