在 Visual C++ 中将 std::string 添加到 uint 时出现不明确的模板错误

发布于 2024-09-24 20:37:27 字数 557 浏览 8 评论 0原文

当我在 Visual Studio 2008 / Windows SDK 7 上编译以下代码时,出现以下错误

const UINT a_uint;
UINT result;

throw std::runtime_error( std::string("we did ") + a_uint +
                          " and got " + result );

讽刺的是,我最终得到了这个结果:

error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(
                 const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
             )' : template parameter '_Elem' is ambiguous

有人可以解释为什么错误消息不能解释真正的问题(没有运算符)用于 +ing 整数到字符串)?

I'm getting the following error when I compile the following code on Visual Studio 2008 / Windows SDK 7

const UINT a_uint;
UINT result;

throw std::runtime_error( std::string("we did ") + a_uint +
                          " and got " + result );

Ironically, I ended up with this result:

error C2782: 'std::basic_string<_Elem,_Traits,_Alloc> std::operator +(
                 const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
             )' : template parameter '_Elem' is ambiguous

Can someone explain why the error message doesn't explain the real problem (that there is no operator for +ing ints to strings)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

牵你的手,一向走下去 2024-10-01 20:37:27

您可以将其简化为

template<typename T>
void f(T, T);

int main() {
 f('0', 0); // is T int or char?
}

您尝试将 unsigned int 添加到字符串中。这是没有意义的,并且 std::string 类不需要采取任何预防措施来在此处添加到 char 的隐式转换,因为这会隐藏此类潜在的编程错误。

尝试将 unsigned int 转换为 std::string 为十进制/十六进制/八进制/等形式,然后连接(您可以使用 std::ostringstream 或 < code>boost::lexical_cast) 或以您认为合适的其他方式修复错误。

You can reduce that to this

template<typename T>
void f(T, T);

int main() {
 f('0', 0); // is T int or char?
}

You try to add an unsigned int to a string. That does not make sense, and the std::string class does not need to take any precautions to add implicit conversions to char here because that would hide such potential programming bugs.

Try to convert the unsigned int to std::string into a decimal/hexadecimal/octal/etc form and then concatenate (you can do that using std::ostringstream or boost::lexical_cast) or fix the bug in other ways you see fit.

妞丶爷亲个 2024-10-01 20:37:27

使用 stringstream (在 sstream 标头中定义)编写错误消息:

std::stringstream ss;
ss << "we did " << a_uint << " and got " << result;
throw std::runtime_error(ss.str());

Use stringstream (defined in the sstream header) to compose the error message:

std::stringstream ss;
ss << "we did " << a_uint << " and got " << result;
throw std::runtime_error(ss.str());
缘字诀 2024-10-01 20:37:27

对于 std::string,您只能在 const char* 指定的地址添加其他 std::string、ASCIIZ 文本,以及单独的char-acters。

要连接其他类型,您可以:

  • 使用流:

    std::ostringstream oss;
    操作系统<< “我们做到了”<< a_uint << ”并得到“<<结果;
    throw std::runtime_error(oss.str());

  • 首先将其转换为字符串表示形式:

    抛出 std::runtime_error(std::string("我们做了") +
    boost::lexical_cast(a_uint) +
    “并得到”+
    boost::lexical_cast(result));

您可能有理由想知道为什么 C++ 不为 { Short, int, long, long long, float, double、unsigned short 等。 },甚至:

template <typename T>
std::string operator+(std::string& s, T& t)
{
    std::ostringstream oss;
    oss << t;
    return s + oss.str();
}

在许多情况下这会很方便。但是流更强大,因为您可以调整填充宽度和字符、浮点精度等。此外,char 是 8 位整数类型,因此编译器如何知道是否要使用该 ASCII 值附加单个字符(例如'A' 代表 65),还是数字 ASCII 值“65”的 ASCII 表示? (目前它不处理任何整数,因此将其视为单个 ASCII char 不会造成混淆)。或者它应该适用于 >=16 位数字但不适用于 8 位数字?这将导致无法在 8 位整数之间调整变量大小,而不必进行复杂的影响分析来查看哪些字符串操作需要重写。最小化依赖关系也是一个很好的做法:使用字符串的一些小但可能很大比例的翻译单元当前可能不需要包含(因此花费时间解析)(因此 ostream 等),并且通常循环依赖关系是“代码味道”并挫败可测试性(字符串取决于 ostringstream 取决于字符串...)。

To a std::string, you can only add other std::strings, ASCIIZ text at an address specified by a const char*, and individual char-acters.

To concatenate other types, you can:

  • use a stream:

    std::ostringstream oss;
    oss << "we did " << a_uint << " and got " << result;
    throw std::runtime_error(oss.str());

  • convert it first to a string representation:

    throw std::runtime_error(std::string("we did ") +
    boost::lexical_cast(a_uint) +
    " and got " +
    boost::lexical_cast(result));

You might reasonably wonder why C++ doesn't provide operator+(std::string&, X&) for X in { short, int, long, long long, float, double, unsigned short etc. }, or even:

template <typename T>
std::string operator+(std::string& s, T& t)
{
    std::ostringstream oss;
    oss << t;
    return s + oss.str();
}

In many cases it would be convenient. But streams are more powerful as you can tune the padding width and character, floating point precision etc.. Further, char is the 8-bit integer type, so how could the compiler know whether to append a single character with that ASCII value (e.g. 'A' for 65), or an ASCII representation of the numeric ASCII value "65"? (Currently it doesn't handle any ints, so treating it as a single ASCII char isn't confusing). Or should it work for >=16 bit numbers but not 8? That would make it impossible to resize variables to/from 8-bit ints without having to do a complex impact analysis to see which string operations needed to be rewritten. It's also good practice to minimise dependencies: some small but perhaps significant percentage of translation units using string may not currently have to include (and hence spend time parsing) (and hence ostream etc), and in general cyclic dependencies are a "code smell" and frustrate testability (string depends on ostringstream depends on string...).

九局 2024-10-01 20:37:27

下次请发布完整的错误(它应该继续“with [ _Elem = ],可能是[不明确重载列表]之一”)。

问题是您将 UINT 与 std::string 连接起来。这不是一个有效的操作,您首先必须将 UINT 转换为 std::string (在 Google 中搜索方便的函数)。编译器会尽力将某些 std::string 运算符与 UINT 进行匹配。显然,它找到了一些匹配项,但这些肯定不是您要寻找的。

Next time please post the full error (it should continue "with [ _Elem = ], could be one of [list of ambiguous overloads]").

The problem is that you concatenate UINT with a std::string. This is not a valid operation, you first have to convert the UINT to a std::string (search Google for handy functions). The compiler is trying to do its best and tries to match some of the std::string operators to the UINT. Apparently, it finds some matches but these certainly aren't what you are looking for.

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