我有使用float类型的默认参数一些麻烦:float类型默认参数=乱码
#include <wchar.h>
#include <iostream>
template<typename T>
void fun(T t = 1e-05);
template<typename T> inline
void fun(T t)
{
std::cout << t << std::endl;
}
int wmain(int argc, wchar_t* argv[])
{
fun<float>();
_getwch();
return 0;
}
它打印-1.36867e-033代替1E-05的等价的。这里发生了什么?
我正在使用VC++ 10。
EDIT1:
谢谢大家的回复。 但铸造默认参数并不在下列情况下工作:
template<typename T>
void fun(T t = static_cast<T>(1e-05));
template<typename T> inline
void fun(T t)
{
std::wcout << t << std::endl;
}
int wmain(int argc, wchar_t* argv[])
{
fun<double>();
fun<float>();
_getwch();
return 0;
}
所以这绝对是一个错误,值得报道?
EDIT2:
Reported this issue to Microsoft
您期待编译器发出从double到float的转换。它没有。使用“1e-5f”修复它。使用connect.microsoft.com报告此问题。 –
也许你需要'wcout'在你的平台上? –
@Hans:微妙。我正要提出'有趣(T t = T(1e-05))',但认为这不是必要的......顺便说一下,它在GCC 4.6上运行良好。 –