- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想更好地学习 C++(目前我的 C++ 仅限于它的 C 子集 *咳* ...),因此我决定尝试“C++ -ify”我的一个有用的日志记录功能,从 C 到 C++,这(我认为)最好用代码解释:
#include <stdarg.h>
#include <stdio.h>
enum msg_type {
LOG_DBG,
LOG_INF,
LOG_WRN,
LOG_ERR
};
int printf_log(enum msg_type mt, const char *fmt_string, ...)
{
va_list args;
va_start(args, fmt_string);
switch (mt) {
case LOG_DBG:
printf("[D] ");
break;
case LOG_INF:
printf("[I] ");
break;
case LOG_WRN:
printf("[W] ");
break;
case LOG_ERR:
printf("[E] ");
break;
default:
break;
}
int res = vprintf(fmt_string, args);
va_end(args);
return res;
}
int main()
{
int i = 0;
printf_log(LOG_DBG, "i is %d\n", i);
i++;
printf_log(LOG_INF, "i is %d\n", i);
i++;
printf_log(LOG_WRN, "i is %d\n", i);
}
这应该输出:
[D] i is 0
[I] i is 1
[W] i is 2
我对此的 C++ 版本的想法是:
#include <log.h>
int main()
{
int i = 0;
log::log(log::dbg)<<"i is " << i << "\n";
i++;
log::log(log::inf)<< "i is " << i << "\n";
i++;
log::log(log::wrn)<< "i is " << i << "\n";
}
同样的结果。
我特别想避免对参数进行任何解析(可能除了 log::inf
或类似参数),而只是让它们直接传递给 cout
.
但我真的不知道从哪里开始,所有与此相关的事情要么需要更多的 C++ 知识,要么希望你自己实现 streambuf
或类似的。
我的想法基本一样,所以我尽力而为,这是我利用互联网上不同资源制作的代码:
#include <iostream>
using std::cout;
class Log {
public:
enum msg_type {
dbg =1,
inf,
wrn,
err
};
Log(enum msg_type mt)
{
switch (mt) {
case dbg:
cout << "[D] ";
break;
case inf:
cout << "[I] ";
break;
case wrn:
cout << "[W] ";
break;
case err:
cout << "[E] ";
break;
default:
break;
}
}
template<typename T>
const Log& operator<<(const T& t)
{
std::cout << t;
return *this;
}
};
int main()
{
int i = 0;
Log(Log::dbg)<< "i is " << i++ << "\n";
Log(Log::inf)<< "i is " << i++ << "\n";
Log(Log::inf)<< "i is " << i++ << "\n";
}
显然它不起作用,但我不知道错误消息试图告诉我什么。
G++:
main.cc: In function ‘int main()’:
main.cc:46:34: error: passing ‘const Log’ as ‘this’ argument discards qualifiers [-fpermissive]
Log(Log::dbg)<< "i is " << i++ << "\n";
^
main.cc:35:14: note: in call to ‘const Log& Log::operator<<(const T&) [with T = int]’
const Log& operator<<(const T& t)
^
main.cc:46:40: error: passing ‘const Log’ as ‘this’ argument discards qualifiers [-fpermissive]
Log(Log::dbg)<< "i is " << i++ << "\n";
^
main.cc:35:14: note: in call to ‘const Log& Log::operator<<(const T&) [with T = char [2]]’
const Log& operator<<(const T& t)
^
clang :
main.cc:46:30: error: invalid operands to binary expression ('const Log' and 'int')
Log(Log::dbg)<< "i is " << i++ << "\n";
~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
main.cc:35:14: note: candidate function not viable: 'this' argument has type
'const Log', but method is not marked const
const Log& operator<<(const T& t)
^
1 error generated.
当然,最简单的方法就是做一个宏,用例如替换我的日志记录“fake-ostream”的任何出现。 cout << [I] <<
但这远没有那么灵活。
有没有人对如何正确执行此操作有任何想法,我可以提示详细描述此操作的资源吗?
感谢任何帮助!
最佳答案
const Log& operator<<(const T& t)
这意味着每次你使用<<
在语句中的日志对象上,const Log&
被退回。下一个链<<
在同一语句中对该 const Log&
进行操作,这不能工作,因为 operator<<
本身没有标记const
.
既然你是在“修改日志”,那么这里有常量就没有多大意义了。 因此,我建议删除 const
完全:
Log& operator<<(const T& t)
现在它看起来像 operator<<
在 std::ostream
中定义( std::cout
的类型),这是有道理的,因为您实际上只是在为 std::cout
创建一个传递.
关于c++ - cout/cerr wrapper ostream 以最小的努力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36531367/
我正在开发一个小型图书馆,我需要做的一件事是让访问者访问一些数据并返回结果。 在一些较旧的 C++ 代码中,访问者需要声明一个 typedef return_type .例如,boost::stati
我不明白这个 ostream 函数声明是什么意思: ostream& operator<< (ostream& (*pf)(ostream&)); (特别是 (*pf)(ostream&) 部分)。我
我正在寻找一种将内容从一个 ostream 复制到另一个的方法。我有以下代码: std::ostringsteam oss; oss << "stack overflow"; { //do s
我有一个装满小雕像的游戏板。 分配: board = new Figure*[boardSize]; for (int i = 0; i > > board(boardSize, vector>(b
代码: cout SomeStream &operator SomeStream &operator (*this) << val; //Trouble in there! std::co
我正在对我的项目进行一些类型检查。下面的例子 using namespace std; cout ::value ? "TRUE":"FALSE" ) (); } 格式正确(source)。 std:
我到处搜索,但没有找到答案,所以如果这是重复的,请原谅。 我有一些非常古老的 C++ 代码,我正试图将它们轻松地转换为这个千年。代码仍然在 Visual Studio 6 中编译并且需要继续这样做,但
在我的 C++ 代码中,我不断地将不同的值写入文件。我的问题是,考虑到文件已成功打开这一事实,如果在任何情况下 write 或 << 会失败。我是否需要检查每次调用 write 或 << 以确保它已正
这个问题在这里已经有了答案: Calling a function overloaded in several namespaces from inside one namespace (3 个答案
我只是在写一些代码来吐出一个 wave header。我开始输入: file > 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com
我有一个派生自 std::ostream 的类(用于日志记录)。剥离下来,它看起来像这样: class bsgs : public std::ostream { public: bsgs(cons
我有以下代码: class A { public: ... C *func() { ... } void func2() { ... } ... }; class B
我在一个抽象类的头文件中找到了这个函数: virtual ostream & print( ostream & out ) const; 谁能告诉我这是什么函数以及如何在派生类中声明它?据我所知,它似
我想写一个函数输出一些东西到 ostream传入并返回流,如下所示: std::ostream& MyPrint(int val, std::ostream* out) { *out << val
偶尔我会写一个类(T 说)并尝试覆盖 std::ostream& operator<<(std::ostream&, const T&) 但它不适用于某些类。这是一个(简化的)类的示例,它对我不起作用
我一直在尝试创建一个程序来为一组已定义的进程实现实时调度算法。使用 g++ 编译时出现错误,其中指出: RTSprocess.h:在函数“std::ostream& operator #include
因此,我得到了一个带有起始代码的任务来实现一个链表(我已经成功地完成了一个未排序的双向链表)并且在给定头文件的起始代码中有一个友元声明似乎有允许我使用 cout 打印链表的目标陈述。这是头文件;请注意
我正在努力 cout using namespace std; ostream& Print(ostream& out) { out using namespace std; ostream&
为了序列化任何对象(即对于没有全局 ostream& operator std::ostream& operator std::string serialize_any(const T& val) {
我宁愿实现一个non-friend函数,直接将函数标记为virtual。 但我现在想确保一组特定的类实现重载 friend std::ostream& operator << (std::ostrea
我是一名优秀的程序员,十分优秀!