std::cout 如果从自定义分配器 (Visual Studio 2019) 调用,则不会输出
std::cout does not output if called from custom allocator (Visual Studio 2019)
我正在尝试研究如何使用自定义分配器。我编写了一个继承自 std::allocator 的基本分配器,并使用 std::cout 尝试跟踪其执行。这是代码:
#include <vector>
#include <iostream>
struct myIntAllocator : std::allocator<int>
{
int* allocate(size_t size)
{
std::cout << "bar" << std::endl;
return new int[size];
}
};
int main()
{
std::cout << "foo" << std::endl;
std::vector<int, myIntAllocator> ints;
ints.push_back(1);
}
输出:
foo
我希望它也输出bar
,但这没有出现。
我是否缺少一些以这种方式std::cout
工作的魔力?还是我误解了如何使用分配器?
我在Visual Studio 2019中使用MSVC v142,编译器优化关闭。
你问:
我是否缺少一些让 std::cout 以这种方式工作的魔力?或有 我误解了如何使用分配器?
在某种程度上,是的 - 通常您不想继承和重载分配器,因为这只会使事情变得更加混乱并使您面临切片/向上投射。另一点是,就我们所知道的,向量或分配器可能有一些我们不知道的实现细节。逻辑解释仍然是你的函数不会被调用。
相关文章:
- 当使用通配符和null指针调用函数时,对输出的说明
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 使用静态变量的递归调用的不同输出
- cmake add_custom_command + Xcode:多输出 = 多命令调用
- 调用 cout 时如何在结构中输出常量文本?
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 函数调用导致没有输出
- 使用 Node.js N-API 调用 C 函数时,不会显示预期的输出
- 如何调用继承的重载运算符<<并在派生类的输出中添加更多文本?
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 空指针(为缺少的输出数组调用的 create() 在 create,文件 /home/m/OpenCV/modules/
- 为什么在杂注 omp 关键之后多次调用 printf 会产生乱码输出?
- std::cout 如果从自定义分配器 (Visual Studio 2019) 调用,则不会输出
- 将 Linux system() 调用命令的输出重定向到仅可变的
- QThread 不提供来自调用 shellscript 的输出
- 显示 matlab 程序的输出,调用另一个 C++ 程序
- 输出 = 0 尝试计算标头中的函数并在主程序中调用它们时
- 基于对函数的参数调用流输出运算符的能力重载函数
- 使用读取系统调用C++输出文本