std::cout 声明如何工作?
How does std::cout declaration work?
std::cout
object 在 iostream
header 文件中声明为
namespace std _GLIBCXX_VISIBILITY(default)
{
...
extern ostream cout;
...
}
从现在开始,我将删除 std::
前缀。
因此,据我了解,cout
是 "just" 类型 ostream
的 object。我开始想知道为什么用 cout
而不是其他 ostream
object 将东西打印到屏幕上,所以我尝试创建一个 object 类型的 ostream
,但编译器不允许我这样做。事实上,ostream
只是 basic_ostream<char>
的别名,该类型的默认构造函数是受保护的。好吧,我想。但后来我想知道:为什么 cout
的声明是合法的?为什么不发出编译器错误?
我再次尝试用关键字extern
声明一个ostream
,例如
extern ostream os;
os << "Will you compile?\n";
这次我得到了 os 的未定义引用错误。然后问题就转移到以下问题:其他地方是否有 'definition' of cout
?如果有,在哪里?
我无法理解这个问题。我知道,出于性能和安全原因,标准 header 是以一种 "cryptic" 的方式编写的,除非您完全掌握该语言(我仍然不了解),否则并不总是容易阅读可能),但我想了解这一点。
谢谢。
I started to wonder why stuff is printed to screen with cout and not with other ostream objects,
因为 std::cout
使用与 stdout
设备关联的流缓冲区。
so I tried to create an object of type ostream, but the compiler would not let me. As a matter of fact, ostream is just an alias for basic_ostream, and the default constructor for that type is protected. Fine, I thought. But then I wondered: why is the declaration of cout legal to start with? Why doesn't that issue a compiler error?
因为它是声明,而不是定义,所以它不调用任何构造函数。也许您应该了解 extern
在声明中的含义。
extern
的声明只是说在程序的其他地方定义了一个类型为 ostream
并称为 cout
的对象。由于您实际上没有提供任何此类定义,因此您会收到链接器错误。
要近似模拟 std::cout
,您可以在某些 .cpp
文件中执行此操作:
namespace {
std::ofstream fake_cout("/dev/stdout");
}
std::ostream cout(fake_cout.rdbuf());
这将创建一个 fstream
,它写入 /dev/stdout
,然后将名为 cout
的 ostream
与其流缓冲区相关联。
这不是对真实 std::cout
的良好模拟,因为它不能确保在任何其他全局变量尝试使用它之前初始化流,而真实的会这样做。
is there a 'definition' of cout somewhere else? If so, where?
是的,在标准C++库的libstdc++实现中有cout
的定义。这个定义不是以标准 C++ 中有效的方式完成的。没关系,因为标准 C++ 库的实现不必用标准 C++ 编写,只需以实现的其余部分理解的方式编写即可。
std::cout
object 在 iostream
header 文件中声明为
namespace std _GLIBCXX_VISIBILITY(default)
{
...
extern ostream cout;
...
}
从现在开始,我将删除 std::
前缀。
因此,据我了解,cout
是 "just" 类型 ostream
的 object。我开始想知道为什么用 cout
而不是其他 ostream
object 将东西打印到屏幕上,所以我尝试创建一个 object 类型的 ostream
,但编译器不允许我这样做。事实上,ostream
只是 basic_ostream<char>
的别名,该类型的默认构造函数是受保护的。好吧,我想。但后来我想知道:为什么 cout
的声明是合法的?为什么不发出编译器错误?
我再次尝试用关键字extern
声明一个ostream
,例如
extern ostream os;
os << "Will you compile?\n";
这次我得到了 os 的未定义引用错误。然后问题就转移到以下问题:其他地方是否有 'definition' of cout
?如果有,在哪里?
我无法理解这个问题。我知道,出于性能和安全原因,标准 header 是以一种 "cryptic" 的方式编写的,除非您完全掌握该语言(我仍然不了解),否则并不总是容易阅读可能),但我想了解这一点。
谢谢。
I started to wonder why stuff is printed to screen with cout and not with other ostream objects,
因为 std::cout
使用与 stdout
设备关联的流缓冲区。
so I tried to create an object of type ostream, but the compiler would not let me. As a matter of fact, ostream is just an alias for basic_ostream, and the default constructor for that type is protected. Fine, I thought. But then I wondered: why is the declaration of cout legal to start with? Why doesn't that issue a compiler error?
因为它是声明,而不是定义,所以它不调用任何构造函数。也许您应该了解 extern
在声明中的含义。
extern
的声明只是说在程序的其他地方定义了一个类型为 ostream
并称为 cout
的对象。由于您实际上没有提供任何此类定义,因此您会收到链接器错误。
要近似模拟 std::cout
,您可以在某些 .cpp
文件中执行此操作:
namespace {
std::ofstream fake_cout("/dev/stdout");
}
std::ostream cout(fake_cout.rdbuf());
这将创建一个 fstream
,它写入 /dev/stdout
,然后将名为 cout
的 ostream
与其流缓冲区相关联。
这不是对真实 std::cout
的良好模拟,因为它不能确保在任何其他全局变量尝试使用它之前初始化流,而真实的会这样做。
is there a 'definition' of cout somewhere else? If so, where?
是的,在标准C++库的libstdc++实现中有cout
的定义。这个定义不是以标准 C++ 中有效的方式完成的。没关系,因为标准 C++ 库的实现不必用标准 C++ 编写,只需以实现的其余部分理解的方式编写即可。