环境:使用 STLPort 5.1.4 的 VS2005 C++。
编译以下代码片段:
std::string copied = "asdf";
char ch = 's';
copied.insert(0,1,ch);
我收到一个错误:
Error 1 error C2668: 'stlpx_std::basic_string<_CharT,_Traits,_Alloc>::insert' : ambiguous call to overloaded function
看来问题出在对字符串对象的插入方法调用上。
定义的两个重载是
void insert ( iterator p, size_t n, char c );
string& insert ( size_t pos1, size_t n, char c );
但鉴于 STLPort 使用简单的 char* 作为其迭代器,我的代码中插入方法中的文字零是不明确的。
所以虽然我可以通过提示轻松解决这个问题,例如
copied.insert(size_t(0),1,ch);
我的问题是:这种重载和可能的歧义在规范中是有意的吗?或者更可能是特定 STLPort 实现的意外副作用?
(注意 Microsoft 提供的 STL 没有这个问题,因为它有一个迭代器类,而不是裸指针)
最佳答案
已知问题,判定为“不是缺陷”。 http://std.dkuug.dk/jtc1/sc22/wg21/docs/lwg-closed.html#84
关于c++ - std::string 插入方法有不明确的重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2819600/