3

When a function does not have any argument, it is possible to call it without parenthesis by define as

#define test test()

Is it possible to call a function with argument without paranthesis? Something like

#define test test(char *arg)

test "argument 1";
4

1 に答える 1

13

That's not going to be possible in C the way you have it. §6.5.2 of the standard (C99) describes postfix expressions, and has no syntax like that. Function calls are (§6.5.2.2):

A postfix expression followed by parentheses () containing a possibly empty, comma-separated list of expressions is a function call. The postfix expression denotes the called function. The list of expressions specifies the arguments to the function.

Parens are not optional, and they need to wrap all the arguments so you need either a function-like macro (which requires parens at the "call" site) or two separate things (one to insert the starting paren, one to insert the closing one).

You could do:

#define test puts(
#define end  );
#define int_proc int
#define proc_body {
#define proc_end  }
#define no_args (void)
#include <stdio.h>

int_proc main no_args
proc_body
  test "hello" end
proc_end

But... really?

C++ offers more possibilities with operator overloading in particular. You might want to look into that if you want to "customize" some syntax.

Here's a horrible example:

#include <iostream>

struct Foo {
    void operator=(char const* str)
    {
        std::cout << str << std::endl;
    }
};
Foo global_foo;

#define test global_foo =

int main()
{
    test "hello";
}

Note that there are sane approaches that you might find attractive, e.g. Qt's qDebug utility class. Schematically, it goes like this:

#include <iostream>

struct debug {
    debug() {}
    ~debug()
    {
        std::cout << std::endl;
    }
    debug const& operator<<(char const* msg) const
    {
        std::cout << msg << " ";
        return *this;
    }
};

The usual way of using it would be:

debug() << "this" << "works";

If you add a constructor that takes char const*:

debug(char const*msg)
{
    std::cout << msg << " ";
}

Then you can use cast notation and write:

(debug) "Hello";

Which is pretty close to what you had (and is macroable).

Then you can get fancy with all the other operators (operator, would be a prime candidate), but precedence rules might ruin the fun a bit.

于 2013-08-20T04:52:52.470 に答える