为什么这个函数重载与参数类型转换不明确
Why is this function overload with argument type conversion ambiguous?
有人可以解释一下这个函数重载分辨率是如何模棱两可的吗?
鉴于:
/// This type *contains* a @c T.
template< typename T >
class User_Type
{
public:
/// This <em>conversion constructor</em> is a key part of it's API;
/// it won't likely change.
User_Type( T const & ar_data )
: m_data( ar_data )
{}
private:
T m_data;
};
/// @c some_value is just a templated function that generates a @c T.
template< typename T > T some_value();
template<> char some_value();
template<> int some_value();
/// This overloaded, non-templated function represents some third-party
/// code somewhere else; it's API can't be changed.
void other_function( User_Type< char > const& );
void other_function( User_Type< int > const& );
/// This is user-code. It's contents exercise some aspect of the 'User_Type' API.
/// This code can change.
template< typename T >
void function()
{
other_function( some_value< T >() ); /* AMBIGUOUS CALL */
User_Type< T > user_var = some_value< T >(); /* UNAMBIGUOUS CONVERSION */
other_function( user_var ); /* UNAMBIGUOUS CALL */
}
template void function< char >();
template void function< int >();
并使用g++-4.9 -Wall -Wextra
进行编译,我收到以下错误:
In instantiation of ‘void function() [with T = char]’:
error: call of overloaded ‘other_function(char)’ is ambiguous
note: candidates are:
note: void other_function(const User_Type<char>&)
note: void other_function(const User_Type<int>&)
In instantiation of ‘void function() [with T = int]’:
error: call of overloaded ‘other_function(int)’ is ambiguous
note: candidates are:
note: void other_function(const User_Type<char>&)
note: void other_function(const User_Type<int>&)
我希望other_function( char )
的最佳匹配是other_function( User_Type< char > const& )
,而other_function( int )
的最佳匹配是other_function( User_Type< int > const& )
。
我知道每个参数都必须进行类型转换才能other_function
. 我希望char
User_Type< char >
是比char
User_Type< int >
更好的选择,char
可以允许int
晋升。 我希望int
User_Type< int >
是比int
User_type< char >
更好的选择,int
可以允许char
转换。
此外,如果我从T
创建本地User_Type< T > user_var
,那么我可以明确地调用other_function( user_var )
。 从语义上讲,这应该等效于第一个原始语句。
由于没有完全匹配的other_function(char)
或other_function(int)
,
other_function( some_value< T >() );
必须隐式转换其参数以匹配以下参数之一:
void other_function( User_Type< char > const& );
void other_function( User_Type< int > const& );
但是User_Type<char>
有接受int
的 CTOR User_Type<char>( char const& )
,User_Type<int>
有接受char
的 CTOR User_Type<int>( int const& )
您的转化属于"用户定义的转化序列"的排名,并且由于这两种转化都是可能的,因此两者都包含在排名相等的重载集中。因此,该调用是模棱两可的。(有关详细信息,请参阅 13.3.3.2 在标准中对隐式转换序列进行排名。
我认为这里的问题是试图根据合格的转换来选择重载。 我可以取出一些模板并获得相同的结果:
class User_Type_char
{
public:
User_Type_char(char const &) {}
};
class User_Type_int
{
public:
User_Type_int(int const &) {}
};
void other_function( User_Type_char const& );
void other_function( User_Type_int const& );
template< typename T >
void function()
{
other_function( 'a' ); /* AMBIGUOUS CALL */
}
问题是重载列表中没有完全匹配,因此我们开始检查转换。 但是,反过来,char
和int
都有隐式转换可用,因此不清楚您想要哪一个。
如果对于相同的代码,我更改了第一个类的构造函数
class User_Type_char
{
public:
User_Type_char(const char*) {}
};
现在,相同的调用变得明确并调用User_Type_int版本。
- 通过引用传递参数时C++类型转换
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 如何在参数中定义隐式类型转换的构造函数?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 将可变参数模板类型转换为 void,预期')'之前
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- 如何定义依赖于参数包转换的函数的返回类型
- C++stoi:这两个重载都无法转换所有参数类型
- 如何类型转换为模板成员函数参数?
- 是否可以将一种函数类型转换为另一种采用相同参数但返回类型不同的函数类型
- 模板参数类型通过转换操作员扣除
- 转换具有不同参数类型的二进制运算函数
- 3个重载都无法转换所有参数类型(在MFC/C++项目中)
- 错误 C2665:'cv::fillPoly':2 个重载都无法转换所有参数类型
- 函子参数和结果的任意类型转换
- 强制转换函数指针,该指针因参数类型而异
- 保存命令行参数并对其进行类型转换
- 为什么这个函数重载与参数类型转换不明确
- 生成错误:12 个重载中没有一个可以转换所有参数类型
- 可视C++参数类型转换