为什么我对以下函数的调用不明确?
请考虑以下事项:
template <typename T>
class testString
{
public:
typedef T* iterator;
void insert(iterator aPos, size_t numChars, T aChar);
testString<T>& insert(size_t aIndex, size_t numChars, T aChar);
};
template <typename T>
void testString<T>::insert( iterator aPos, size_t numChars, T aChar )
{
}
template <typename T>
testString<T>& testString<T>::insert( size_t aIndex, size_t numChars, T aChar )
{
return *this;
}
int main()
{
testString<char> c;
c.insert(0, 10, 'a'); // ambiguous call
c.insert(1, 10, 'a'); // compiles fine
return 0;
}
为什么我会接到含糊不清的电话?最初,我有一个猜测(因为它是 0,所以它可以是任何东西),但后来我查看了 std::string
。我查看了源代码,这是两个函数:
void insert ( iterator p, size_t n, char c );
string& insert ( size_t pos1, size_t n, char c );
我尝试使用 std::string 进行相同的调用,并且工作正常。现在我不知道为什么 0
与 std::string
一起使用并立即被识别为 size_t
而在我的实现中它不是(另一个猜测是类型特征,但来源中没有证据)?
Consider the following:
template <typename T>
class testString
{
public:
typedef T* iterator;
void insert(iterator aPos, size_t numChars, T aChar);
testString<T>& insert(size_t aIndex, size_t numChars, T aChar);
};
template <typename T>
void testString<T>::insert( iterator aPos, size_t numChars, T aChar )
{
}
template <typename T>
testString<T>& testString<T>::insert( size_t aIndex, size_t numChars, T aChar )
{
return *this;
}
int main()
{
testString<char> c;
c.insert(0, 10, 'a'); // ambiguous call
c.insert(1, 10, 'a'); // compiles fine
return 0;
}
Why am I getting an ambiguous call? Initially, I had a guess (because it is 0, it can be anything), but then I looked at std::string
. I looked at the source, and these are the two functions:
void insert ( iterator p, size_t n, char c );
string& insert ( size_t pos1, size_t n, char c );
And I tried the same calls with std::string, and it works fine. Now I have no clue why 0
works with std::string
and is immediately recognized as a size_t
while in my implementation it is not (another guess was type traits, but there is no evidence of it in the source)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,有一次你会想“如果在 C++ 中,0 与 null 不同不是更好吗?”
不管怎样,你的迭代器类型被 typedef'd 为 T*,即 char*,而 0 是 char* 的一个很好的值,因此存在歧义(呃,正如 K-ballo 所写)。在 std::string 中,迭代器类型不会是 char*,它会像另一个(模板化)类一样奇特......
Ah, one of those times when you're like "wouldn't it be better if in c++, 0 was not the same as null?"
Anyway, your iterator type is typedef'd to T*, which is char*, and 0 is a fine value for a char*, hence the ambiguity (er, as K-ballo wrote). In std::string, the iterator type won't be a char*, it'll be something fancy like another (templatized) class...
由于
0
既可以被视为指针,也可以被视为整数,因此会产生歧义。如果您想区分它们,我的建议是将您的
T *
包装在testClass
内的class
中。这类似于:如果您注意到,对于 STL,您也不能将整数值作为迭代器发送,但必须传递
whateverclass::iterator
类型的迭代器。Since
0
could be treated both as a pointer and an integer, you get the ambiguity.If you want to distinguish them, my suggestion would be to wrap your
T *
in aclass
insidetestClass
. That is something like:If you have noticed, to STL either, you can't send an integer value as iterator, but you must pass an iterator of type
whateverclass<whatevertype>::iterator
.