这段 C++ 代码的奇怪行为(std::wcout 和 std::exception)
strange behaviour of this c++ piece of code (std::wcout and std::exception)
我正在使用这段代码,我发现该方法在成功调用后不会引发异常。如果我使用 std::cout,一切都很好,并且会抛出异常。我使用的是 gcc 版本 4.9.2(Debian 4.9.2-10(。是 gcc 错误还是 stl 错误代码问题或其他什么?
// exceptions
#include <iostream>
using namespace std;
class C {
public:
string srch(int &i) {
if (i == 0) { //found
wcout << "got it: " << i << endl; return "i";
}
throw std::exception();
}
};
int main () {
C c = C();
int i = 2;
int j = 0;
try
{
c.srch(j);
c.srch(i);
}
catch (const std::exception &e) {
cout << "An exception occurred. Exception Nr. " << e.what() << 'n';
}
return 0;
}
这是一个 ideone 链接,用于重现缺少异常wcout
.以及一个在使用cout
时重现异常的链接。
您的示例不能证明未引发异常。
catch 块中的cout
消息不会显示,因为您已经使用了wcout
并且在同一设备上混合字符宽度 (stdout( 是未定义的行为。
将cout
更改为wcout
,您将看到引发异常,只是没有看到预期的消息。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用std::wcout会降低奇怪的性能
- Printing CComBSTR with iostream (std::wcout)
- 这段 C++ 代码的奇怪行为(std::wcout 和 std::exception)
- 混合std :: wcout and std :: cout会犯错,什么错
- 如何根据定义的字符串类型在“std::cout”和“std::wcout”之间进行选择
- 如何使用 std::imbue 来设置 std::wcout 的区域设置
- std::wcout奇怪错误:std::wstring的输出被截断
- c++ wcout std::map value
- std::cout和std::wcout的区别是什么?
- C++中使用std::wcout的高阶函数失败,错误为C2248