【发布时间】:2014-10-24 09:26:58
【问题描述】:
正如您在此处看到的 http://melpon.org/wandbox/permlink/vJSyO14mkbH0MQRq 这不会在 gcc 上编译并出现错误:
prog.cc: In instantiation of 'constexpr B convert(A) [with A = unsigned char; B = short unsigned int]':
prog.cc:16:52: required from here
prog.cc:12:1: error: body of constexpr function 'constexpr B convert(A) [with A = unsigned char; B = short unsigned int]' not a return-statement
代码:
#include <stdint.h>
#include <limits>
#include <iostream>
template< typename A, typename B >
constexpr B convert( A a )
{
auto aMax = std::numeric_limits< A >::max();
auto bMax = std::numeric_limits< B >::max();
return a * ( bMax / aMax );
}
int main()
{
std::cout << convert< uint8_t, uint16_t >( 128 ) << std::endl;
return 0;
}
【问题讨论】: