【问题标题】:g++ integer to to string assignment without an error/warningg++ 整数到字符串赋值,没有错误/警告
【发布时间】:2012-04-16 12:55:09
【问题描述】:

以下代码在 G++ (GCC) 4.1.2 上编译时没有任何警告或错误。

是否有不发出错误/警告的原因?

是否有一个标志可以导致它发出警告或错误?

#include <iostream>

using namespace std;

void func(string &s)
{
    unsigned long u = 123;
   s = u;
}

int main()
{
   string s;

   func (s);

   return 0;
}

【问题讨论】:

    标签: gcc g++ gcc-warning


    【解决方案1】:

    没有警告或错误的原因是因为这是有效的 C++。 std::string 重载赋值运算符 (operator=) 以获取 charlong 可以隐式转换为char,因此代码是有意义的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-21
      • 2012-03-28
      • 2023-03-31
      • 2015-10-13
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2015-10-19
      相关资源
      最近更新 更多