當我編譯程序使用g ++ 5.1,我得到:
/home/imk/develop/so/mpf_cast/main.cpp:35:25: error: call of overloaded ‘__gmp_expr(q&)’ is ambiguous
cout << (mpf_class) x << endl;
^
/home/imk/develop/so/mpf_cast/main.cpp:35:25: note: candidates are:
In file included from /home/imk/develop/so/mpf_cast/main.cpp:2:0:
/usr/include/gmpxx.h:1883:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(double)
__gmp_expr(double d) { mpf_init_set_d(mp, d); }
^
/usr/include/gmpxx.h:1880:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(float)
__gmp_expr(float f) { mpf_init_set_d(mp, f); }
^
/usr/include/gmpxx.h:1876:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(long unsigned int)
__gmp_expr(unsigned long int l) { mpf_init_set_ui(mp, l); }
^
/usr/include/gmpxx.h:1873:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(long int)
__gmp_expr(signed long int l) { mpf_init_set_si(mp, l); }
^
/usr/include/gmpxx.h:1869:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(short unsigned int)
__gmp_expr(unsigned short int s) { mpf_init_set_ui(mp, s); }
^
/usr/include/gmpxx.h:1866:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(short int)
__gmp_expr(signed short int s) { mpf_init_set_si(mp, s); }
^
/usr/include/gmpxx.h:1862:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(unsigned int)
__gmp_expr(unsigned int i) { mpf_init_set_ui(mp, i); }
^
/usr/include/gmpxx.h:1859:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(int)
__gmp_expr(signed int i) { mpf_init_set_si(mp, i); }
^
/usr/include/gmpxx.h:1855:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(unsigned char)
__gmp_expr(unsigned char c) { mpf_init_set_ui(mp, c); }
^
/usr/include/gmpxx.h:1852:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(signed char)
__gmp_expr(signed char c) { mpf_init_set_si(mp, c); }
^
/usr/include/gmpxx.h:1837:3: note: __gmp_expr<__mpf_struct [1], __mpf_struct [1]>::__gmp_expr(const __gmp_expr<__mpf_struct [1], __mpf_struct [1]>&)
__gmp_expr(const __gmp_expr &f)
^
問題投表達(mpf_class) x
需要mpf_class
(typedef __gmp_expr<mpf_t, mpf_t> mpf_class
) 將要從q
構成。所以你也可以考慮
q x(1);
mpf_class m(x);
這會引發相同的診斷。
爲什麼施工模糊不清?這是因爲: -
- 還有就是,當然,沒有這樣的構造如
mpf_class(q const &)
- 因此,轉換是必需的,如果存在的話,從
q
在診斷中列舉的其他11種類型之一, ,從中可以構建出mpf_class
可以。
- 但也有這樣的轉換,每個不如對方,這要歸功於 兩個轉換操作符與您所提供
q
。
轉換operator double()
能滿足第一個11個構造 的和operator mpf_class()
能夠滿足最後一個。編譯器沒有基礎 喜歡這些構造函數。
如果你犧牲了一個或其他演員,這個問題就會消失。 如果你都必須有他們,那麼你也可以通過使二者 explicit
的解決這個問題,所以,除非它被顯式調用編譯器將不考慮調用轉換:
explicit operator double()
{
return (double)i;
}
explicit operator mpf_class()
{
return (mpf_class)i;
}
這時你會發現,例如:
int main()
{
q x(1);
mpf_class mpf(x); // via `explicit operator mpf_class()`
double d(x); // via `explicit operator double()`
// d = q(2); <- does not compile
d = static_cast<double>(q(2));
// mpf = q(2); <- does not compile
mpf = static_cast<mpf_class>(q(2));
return 0;
}
順便說一句:
- 演員運營商應該是
const
- 在一個函數中,當存在隱式轉換時,將返回值轉換爲 返回類型是非常繁瑣的。
因此:
explicit operator double() const
{
return i;
}
explicit operator mpf_class() const
{
return i;
}
它也可以,如果我刪除投翻一番,但反過來就意味着損失性能的時候我只是想轉換爲雙。 –
如何定義「mpf_class」?它是什麼? – user35443
mpf_class是GMP(Gnu Multiple Precision)庫中的主要類之一。 –