[英]C++ ambiguous overload and constructor with defaults
我有一個程序,其代碼可以完美地運行,但是編譯器仍然會輸出以下令人討厭的警告:
warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
我的程序的簡化版本是:
#include <iostream>
#include <iomanip>
class point
{
public:
point( int x = 0, int y = 0 )
: _x( x ), _y( y )
{}
point( const point &p )
: _x( p._x ), _y( p._y )
{}
int & x( ) { return _x; }
int & y( ) { return _y; }
private:
int _x, _y;
};
class matrix
{
public:
int operator()( int x, int y ) const
{ return _array[ index( x, y ) ]; }
int operator()( point< int > p ) const
{ return operator()( p.x( ), p.y( ) ); }
int operator()( int x, int y, int value )
{
_array[ index( x, y ) ] = value;
return _array[ index( x, y ) ];
}
int operator()( point< int > p, int value )
{ return operator()( p.x( ), p.y( ), value ); }
private:
int _array[ 4 * 5 ];
int index( int x, int y ) const
{ return y * Width + x; }
};
int main( )
{
std::cout << "Filling matrix." << std::endl;
matrix< int, 4, 5 > m;
for ( int y = 0; y < 5; ++y )
for ( int x = 0; x < 4; ++x )
{
m( x, y, ( y * 4 + x ) );
}
std::cout << "Reading matrix." << std::endl;
for ( int y = 0; y < 5; ++y )
{
std::cout << std::endl << "|";
for ( int x = 0; x < 4; ++x )
{
std::cout << std::setw( 3 ) << std::setfill( ' ' ) << m( x, y ) << " |";
}
}
std::cout << std::endl << "Done." << std::endl;
}
我看不到operator()
重載有什么問題。 有任何想法嗎?
好,
我花了一些時間弄清楚此警告的實際原因。 由於我的程序運行良好,所以直到今天我都忽略了該警告,並且認為共享它很有用,這樣世界各地的其他人可以節省一些時間。
在對operator()
構造進行了很多修改之后,我決定進行一個向上的研究,想知道為什么我的編譯器會將單個值與一個point
混淆,並將第二個參數作為value
。
而且我發現我已經為我的point
構造函數延遲添加了默認值,因此我也可以將其用作默認構造函數。 當我添加此默認值時,我沒有注意到任何人都可以通過僅省略第二個參數來使用此構造函數,這導致了我的錯誤。
只是為了好笑,我改變了我的構造函數如下:
#ifdef AMBIGUOUS
point( int x = 0, int y = 0 )
#else
point( )
: point( 0, 0 ) {}
point( int x, int y )
#endif
然后,我可以嘗試使用以下解決方案進行清理:
> g++ ambig_param.cpp -o ambig_param -Wall -std=c++14
好。 稍后編譯時沒有問題:
> g++ ambig_param.cpp -o ambig_param -Wall -std=c++14 -DAMBIGUOUS
ambig_param.cpp: In function ‘int main()’:
ambig_param.cpp:68:66: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
std::cout << std::setw( 3 ) << std::setfill( ' ' ) << m( x, y ) << " |";
^
ambig_param.cpp:27:4: note: candidate 1: T matrix<T>::operator()(T, T) const [with T = int]
T operator()( T x, T y ) const
^~~~~~~~
ambig_param.cpp:39:4: note: candidate 2: T matrix<T>::operator()(point<int>, T) [with T = int]
T operator()( point< int > p, T value )
^~~~~~~~
我希望有一天能對某人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.