Call function with arguments without parenthesis in C? -
when function not have argument, possible call without parenthesis define
as
#define test test()
is possible call function argument without paranthesis? like
#define test test(char *arg) test "argument 1";
that's not going possible in c way have it. §6.5.2 of standard (c99) describes postfix expressions, , has no syntax that. function calls (§6.5.2.2):
a postfix expression followed parentheses
()
containing possibly empty, comma-separated list of expressions function call. postfix expression denotes called function. list of expressions specifies arguments function.
parens not optional, , need wrap arguments need either function-like macro (which requires parens @ "call" site) or 2 separate things (one insert starting paren, 1 insert closing one).
you 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 operator overloading in particular. might want if want "customize" syntax.
here's 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 there sane approaches might find attractive, e.g. qt's qdebug
utility class. schematically, goes 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 be:
debug() << "this" << "works";
if add constructor takes char const*
:
debug(char const*msg) { std::cout << msg << " "; }
then can use cast notation , write:
(debug) "hello";
which pretty close had (and macroable).
then can fancy other operators (operator,
prime candidate), precedence rules might ruin fun bit.
Comments
Post a Comment