为什么我对以下函数的调用不明确?

发布于 2024-12-05 09:43:50 字数 1061 浏览 0 评论 0原文

请考虑以下事项:

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 进行相同的调用,并且工作正常。现在我不知道为什么 0std::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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2024-12-12 09:43:50

啊,有一次你会想“如果在 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...

一枫情书 2024-12-12 09:43:50

由于 0 既可以被视为指针,也可以被视为整数,因此会产生歧义。

如果您想区分它们,我的建议是将您的 T * 包装在 testClass 内的 class 中。这类似于:

template <typename T>
class testString
{
public:
  class iterator
  {
  public:
    T *addr;
  }
  void insert(iterator aPos, size_t numChars, T aChar);
  testString<T>& insert(size_t aIndex, size_t numChars, T aChar);
};

如果您注意到,对于 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 a class inside testClass. That is something like:

template <typename T>
class testString
{
public:
  class iterator
  {
  public:
    T *addr;
  }
  void insert(iterator aPos, size_t numChars, T aChar);
  testString<T>& insert(size_t aIndex, size_t numChars, T aChar);
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文