在C+中是否有现成的cout替代方案+;其行为与qDebug类似? 我喜欢用Qt:使用 qDebug G/>代码>编写C++中控制台输出的变量是多么容易 int a = b = c = d = e = f = g = 1; qDebug() << a << b << c << d << e << f << g;

在C+中是否有现成的cout替代方案+;其行为与qDebug类似? 我喜欢用Qt:使用 qDebug G/>代码>编写C++中控制台输出的变量是多么容易 int a = b = c = d = e = f = g = 1; qDebug() << a << b << c << d << e << f << g;,c++,qt,C++,Qt,相比之下,使用std::cout需要我手动添加间距和换行,以获得相同的结果: std::cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " << g << "\n"; 应返回: 1 2 3 4 5 6 struct debug

相比之下,使用
std::cout
需要我手动添加间距和换行,以获得相同的结果:

std::cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " << g << "\n";
应返回:

1 2 3
4 5 6
struct debugcout{};
模板
debugcout&操作员
#包括
类myDebug{
布尔首先是{true};
布尔是最后一个{true};
公众:
myDebug()=默认值;
myDebug(myDebug const&)=delete;
myDebug&operator=(myDebug常量&)=delete;
myDebug&operator=(myDebug&&)=删除;
myDebug(myDebug&&dc)无例外
:is_first{false}{
dc.is_last=false;
}
~myDebug(){
如果(是最后一个)

STD:Cube中没有C++内置,AFAIK。我会更新问题使它更清楚,但我所寻找的是理想的库(或片段)。当我需要这样的东西时,我总是可以使用它作为我的解决方案。它可以是头文件,也可以是一个大型的日志库,被大量使用并经过良好测试。它也可以是一个小片段。关键是它应该足够小和/或标准,以便其他合作者可以将它包含在项目中,只用于调试/日志这可能是重复的。谢谢!这很接近我想要的,但它缺少自动换行插入。这可能很难在几行中实现,但我可能错了。此外,内联变量似乎没有使用GCC 5.4编译,即使指定了C++1z/C++17。@dragly这是一个C++17 fe名为内联变量的变量。
myDebug << 1 << 2 << 3;
myDebug << 4 << 5 << 6;
1 2 3
4 5 6
struct debugcout { };

template <typename T>
debugcout& operator<<(debugcout& os, const T& x)
{
    std::cout << x << ' ';
    return os;
}

inline debugcout debug{};
int main()
{
    debug << 1 << 2 << 3;
}
#include <iostream>

class myDebug {
    bool is_first{true};
    bool is_last{true};
public:
    myDebug() = default;
    myDebug(myDebug const &) = delete;
    myDebug & operator = (myDebug const &) = delete;
    myDebug & operator = (myDebug &&) = delete;
    myDebug(myDebug && dc) noexcept 
      : is_first{false} {
        dc.is_last = false;
    }
    ~myDebug() {
        if (is_last)
            std::cout << '\n';
    }
    template <typename T>
    friend myDebug operator<<(myDebug db, const T& x) {
        if (db.is_first)
            db.is_first = false;
        else
            std::cout << ' ';

        std::cout << x;
        return db;
    }
};
int main() {
    myDebug() << 1 << 2 << 3;
    myDebug() << 4 << 5 << 6;
}