我有使用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上運行良好。 –