C++ 的调试宏带有变量参数但不带格式字符串

发布于 2024-12-22 19:47:30 字数 535 浏览 3 评论 0原文

是否可以编写一个宏,它可以接受可变数量的参数并像这样扩展:

quickdebug(a)   ->  cout << #a ": " << a;
quickdebug(a,b) ->  cout << #a ": " << a << #b ": "<< b;

如果没有,我是否可以至少打印所有参数而不给出格式字符串。例如

quickdebug2(a)   -> cout << a ;
quickdebug2(a,b) -> cout << a << " " << b ;

在java中,我可以编写一个为我提供类似功能的函数:

void debug(Object...args) 
{
  System.out.println(Arrays.deepToString(args));
}

Is it possible to write a macro which can take in a variable number of arguments and expands like this :

quickdebug(a)   ->  cout << #a ": " << a;
quickdebug(a,b) ->  cout << #a ": " << a << #b ": "<< b;

etc

If not, is it possible for me to at least print all the arguments without giving format strings. e.g

quickdebug2(a)   -> cout << a ;
quickdebug2(a,b) -> cout << a << " " << b ;

etc

For example in java I can write a function which provides me similar functionality:

void debug(Object...args) 
{
  System.out.println(Arrays.deepToString(args));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

已下线请稍等 2024-12-29 19:47:30

通过使用覆盖 , 运算符的类:

class VariadicToOutputStream
{
public:
    VariadicToOutputStream(std::ostream& s, const std::string& separator = " ") : m_stream(s), m_hasEntries(false), m_separator(separator) {}
    template<typename ObjectType>
    VariadicToOutputStream& operator , (const ObjectType& v)
    {
        if (m_hasEntries) m_stream << m_separator;
        m_stream << v;
        m_hasEntries=true;
        return *this;
    }
    ~VariadicToOutputStream()
    {
        m_stream << std::endl;
    }

private:
    std::ostream& m_stream;
    bool m_hasEntries;
    std::string m_separator;
};

例如,您可以编写:

VariadicToOutputStream(std::cout) , 1, 0.5f, "a string";

然后可以用预处理器宏包装:

#define VARIADIC_TO_STDOUT(...)     VariadicToOutputStream(std::cout),__VA_ARGS__;

因此您可以编写:

VARIADIC_TO_STDOUT(1, 0.5f, "a string");

添加要在参数之间使用的 fi 分隔符字符串会很容易。

编辑:我刚刚添加了一个默认空格作为分隔符字符串。

By using a class that overrides , operator:

class VariadicToOutputStream
{
public:
    VariadicToOutputStream(std::ostream& s, const std::string& separator = " ") : m_stream(s), m_hasEntries(false), m_separator(separator) {}
    template<typename ObjectType>
    VariadicToOutputStream& operator , (const ObjectType& v)
    {
        if (m_hasEntries) m_stream << m_separator;
        m_stream << v;
        m_hasEntries=true;
        return *this;
    }
    ~VariadicToOutputStream()
    {
        m_stream << std::endl;
    }

private:
    std::ostream& m_stream;
    bool m_hasEntries;
    std::string m_separator;
};

You can write for instance:

VariadicToOutputStream(std::cout) , 1, 0.5f, "a string";

This can then be wrapped with a preprocessor macro:

#define VARIADIC_TO_STDOUT(...)     VariadicToOutputStream(std::cout),__VA_ARGS__;

So you can write:

VARIADIC_TO_STDOUT(1, 0.5f, "a string");

It would be easy to add f.i. separator strings to be used between arguments.

Edit: I just added a default space as separator string.

仲春光 2024-12-29 19:47:30

可以创建一个可变参数的宏,从而采用可变数量的参数。语法类似于函数的语法:

#define quickdebug(...) functiontocall("test", __VA_ARGS__)

参数列表中最后一个命名参数之后列出的任何参数都将列在 __VA_ARGS__ 中,包括任何分隔逗号。

所以: quickdebug(1, 2, "123", 4.5) 变成 functioncall("test", 1, 2 , "123", 4.5)

但是在某些时候你需要使用这些参数,如果没有格式字符串或其他指示参数类型的东西,这可能会变得非常困难。

问题是,当从变量参数列表中读取变量时,您需要知道参数的类型,或者至少知道它的大小。如果我是你,我会选择不同的方法。

您可以在此处阅读有关可变参数宏的更多信息: http://gcc.gnu.org/ onlinedocs/cpp/Variadic-Macros.html

It is possible to make a macro that is variadic thus taking a variable amount of arguments. The syntax is similar to that of a function:

#define quickdebug(...) functiontocall("test", __VA_ARGS__)

Any argument listed after the last named argument in the argument list will be listed in __VA_ARGS__ including any seperating comma.

So: quickdebug(1, 2, "123", 4.5) becomes functioncall("test", 1, 2 , "123", 4.5)

However at some point you need to use these arguments, and here it can become extremely difficult if you don't have a format string, or something else indicating the type of the arguments.

The problem is that when reading variables from a variable arguments list, you need to know the type of the argument, or at least its size. If I were you I would choose a different approach.

You can read more about variadic macros here: http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文