All Questions

Tagged with
Filter by
Sorted by
Tagged with
89 votes
5 answers
8k views

What's the significance of a C function declaration in parentheses apparently forever calling itself?

In gatomic.c of glib there are several function declarations that look like this: gboolean (g_atomic_int_compare_and_exchange_full) (gint *atomic, gint ...
Andreas's user avatar
  • 9,549
48 votes
4 answers
46k views

Empty function macros

If I define a function macro with no actual body, is it like an empty string with the compiler (i.e. It doesn't generate any extra instructions at compile time)? Example: #define SomeMacro(a) ...
Qix - MONICA WAS MISTREATED's user avatar
43 votes
6 answers
21k views

Python-like C++ decorators

Are there ways to decorate functions or methods in C++ like in python style? @decorator def decorated(self, *args, **kwargs): pass Using macros for example: DECORATE(decorator_method) int ...
Artem Selivanov's user avatar
33 votes
6 answers
55k views

Implement generic swap macro in C [duplicate]

Possible Duplicate: is there an equivalent of std::swap() in c Hi folks, I was attempting a problem to write a generic swap macro in C and my macro looks like this: #define swap(x,y) { x = x + ...
Mohit Dhuper's user avatar
20 votes
6 answers
66k views

What is the difference between a macro and a function in C? [closed]

What is the difference between a macro and a function in C? Please tell me one application where I can use macros and functions?
user615929's user avatar
19 votes
2 answers
3k views

elisp macro to write a function?

I have written a few nearly identical functions, except for their names. For example: ; x is name, such as function/paragraph/line/etc. (defun my-x-function (interactive) (mark-x) (do-more-stuff) (...
Anycorn's user avatar
  • 50.9k
14 votes
2 answers
5k views

Why does GCC keep empty functions?

In most cases if I want to create an optional feature in C, I simply create two functions like this: #ifdef OPTIONAL_SOMETHING void do_something(int n, const char *s) { while (n--) { ...
Ákos Kovács's user avatar
11 votes
7 answers
2k views

How to know what function called another

I wanna know if there is any way to know where the function currently in execution was called, this is, in what file and line. I'm using C language, and I'm looking for something similar to ...
Sérgio's user avatar
  • 1,093
11 votes
2 answers
617 views

At a language level, what exactly is `ccall`?

I'm new to Julia, and I'm trying to understand, at the language level, what ccall is. At the syntax level, it looks like a normal function, but it clearly doesn't behave the same way in how it takes ...
lcmylin's user avatar
  • 2,642
10 votes
1 answer
879 views

Clojure, can macros do something that couldn't be done with a function

I'm learning Clojure macros, and wonder why we can't use just functions for metaprogramming. As far as I know the difference between macro and function is that arguments of macro are not evaluated ...
Tuomas Toivonen's user avatar
9 votes
5 answers
3k views

Is there a apply-function-to-region-lines in emacs?

A lot of my work involves searching and deleting unnecessary lines of code. So I create a macro, and then select all lines (C-x h) and then run the command (apply-macro-to-region-lines). I managed to ...
dola's user avatar
  • 201
8 votes
6 answers
5k views

What's the safest way to define short function name aliases in C++?

Suppose I have a class Utility in a file utility.h: class Utility { public: static double longDescriptiveName(double x) { return x + 42; } }; And then I find that I use the function ...
Alan Turing's user avatar
  • 12.4k
8 votes
2 answers
2k views

Difference between a macro definition and function definition

I'm trying to learn Lisp but I got stuck by this example (you can find it on "ANSI Common Lisp", by Paul Graham, page 170): (defmacro in (obj &rest choices) (let ((insym (gensym))) `(...
vrde's user avatar
  • 937
7 votes
2 answers
489 views

Macro within macro in C

Please help me solving this problem. This is sample code to get line number,file name and var args in C. When I tried to run this, I got some errors. I am sure there are many ways to do this. But I ...
Rock26's user avatar
  • 189
6 votes
1 answer
6k views

Zipping lists together in Common Lisp - Problem with "and"

What I am trying to do is create a function zip (note now that this is not homework) that iterates through multiple lists simultaneously, applying a function to each list of elements, like so: (zip f ...
Aaa's user avatar
  • 1,854
6 votes
4 answers
391 views

Is this valid usage of inline functions?

Let's say I have this code (don't mind the fact that SecondsToMinutes and MinutesToHours are carbon copies of each other) inline float SecondsToMinutes(float seconds) { return seconds / 60.0; } ...
Stephanus Tavilrond's user avatar
6 votes
3 answers
263 views

Rewriting a c++ macro as a function, etc

I have a macro that I use a lot, inspired by another question: #define to_string(x) dynamic_cast<ostringstream &> (( ostringstream() << setprecision(4) << dec << x )).str()...
jorgen's user avatar
  • 3,505
6 votes
3 answers
102 views

C-style macros in R

In C, a macro is like a function, but before compilation, any instance of the macro call is replaced by the text of the macro. My use-case is this: floor_dec <- function(x, sigDig=1) { if (x == 0)...
Jeff's user avatar
  • 694
6 votes
3 answers
84 views

Need help regarding macro definition

Im reading c++ code, i have found such definition #define USE_VAL(X) if (&X-1) {} has anybody idea, what does it mean?
Waleed A's user avatar
6 votes
1 answer
574 views

How to pass `and` as a function in Racket?

For the following code: (foldl and #t '(#t #f)) Racket returns: and: bad syntax in: and I know and is not a function. And I can circumvent this problem using lambda: (foldl (lambda (a b) (and a b)...
Ben's user avatar
  • 3,822
6 votes
2 answers
773 views

getc() as macro and C standard library function definition, coherent?

In [7.1.4 Use of library functions], I read : Any function declared in a header may be additionally implemented as a function-like macro defined in the header... and Any invocation of a ...
Jean-Baptiste Yunès's user avatar
6 votes
4 answers
2k views

Objective-C: Function-Like Macro Vs. Method

In Objective-C, when do you recommend using function-like macros over class or instance methods?
ma11hew28's user avatar
  • 124k
6 votes
2 answers
6k views

Passing macro arguments to macro function

How do I pass macro arguments along with another integer variable to a macro function? #define SUM(X, Y, Z) X + Y + Z #define FOO 1, 2 void print(int a, int b) { printf("Sum: %d", a + b); } int ...
heethesh's user avatar
  • 371
5 votes
5 answers
9k views

ABS(A) and abs(int)

I am baffled in the difference of this two in xcode, ABS(A) and abs(int). Cant seem to find ay explanation online as well. Which should I use? I am actually working on an accelerometer. Using ABS(A) ...
Hexark's user avatar
  • 413
5 votes
1 answer
1k views

How to dynamically call an operator in Elixir

I'm working my way through Dave's upcoming book on Elixir, and in one exercise I would like to dynamically construct a function reference to Kernel.+/2, Kernel.-/2 etc, based on the contents of one ...
Daniel Ashton's user avatar
5 votes
2 answers
301 views

Is isupper() a macro or a function?

I was reading a book on c++ (A Complete Guide to Programming in C++ by Ulla Kirch-Prinz and Peter Prinz; ISBN: 0-7637-1817-3) and it mentioned that isupper(), along with islower(), isalpha(), isdigit()...
thepufferfish's user avatar
5 votes
2 answers
5k views

Disable functions using MACROS

After searching quite a bit in the Internet for a solution I decided to ask here if my solution is fine. I'm trying to write a simple and modular C logging library intended to be simple to disable ...
Armando's user avatar
  • 53
5 votes
2 answers
5k views

Is there a way to perform function inlining in MATLAB?

What language feature or outside-the-box hack can I use to accomplish function inlining in MATLAB? Annoyingly, a Google search for "matlab inline function" reveals that MATLAB's designers thought that ...
AlcubierreDrive's user avatar
5 votes
2 answers
1k views

Macro in C to call a function returning integer and then return a string

I have a function which returns an integer value. Now I want to write a macro which call this function, gets the return value and prepends a string to it and return the resultant string. I have tried ...
iqstatic's user avatar
  • 2,342
4 votes
6 answers
2k views

macro and function conflict in C

What error is thrown when macro and function conflict in C arises? Is it a macro processor error or does this error occur due to some language violation? For example, in this code: #include <...
Mahesh Gupta's user avatar
4 votes
3 answers
429 views

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...
Nick Orton's user avatar
  • 3,623
4 votes
2 answers
2k views

How to pass arguments to a variadic macro?

I have a variadic function: LogWrite(FILE * fp, int level, const char * filename, const char * function, ...) It should be called like this: LogWrite(fp, int Level, __FILE__, __FUNCTION__, "Message:...
Sagar's user avatar
  • 9,474
4 votes
2 answers
356 views

Haxe Macros - replace "function" with "async function"

I want that when converting haxe to JavaScript, asynchronous is added to its methods. I have Haxe code: @:expose class Main implements IAsync { static function main() { trace("test");...
Alex's user avatar
  • 153
4 votes
3 answers
123 views

How to construct a function from a list of functions and a value?

I'm trying to build a function out of the results of one already evaluated function and a list of other functions. If I partially construct it it works fine, but every time I try to construct the ...
user avatar
4 votes
1 answer
618 views

How to check for accept4 using CMake

I am checking for accept4 on Linux like this: check_symbol_exists(accept4 sys/socket.h HAVE_ACCEPT4) However, accept4 is only defined if _GNU_SOURCE is defined beforehand. How can I force the ...
Martin Sustrik's user avatar
4 votes
2 answers
824 views

Macros and functions in Clojure

I read the following line in this Clojure tutorial - http://java.ociweb.com/mark/clojure/article.html#Macros 'Since macros don't evaluate their arguments, unquoted function names can be passed to ...
Pranav's user avatar
  • 3,322
3 votes
2 answers
793 views

How to tag text in emacs

I'm developing a text annotation system in emacs, where the format of the annotation is something like this. If this is the text: Lorem ipsem por favor I need to annotate it like this: {latin}...
singingfish's user avatar
  • 3,146
3 votes
2 answers
103 views

How can I make sure I will use the function version of a C standard library function like "getc" instead of the function-like macro version?

I was learning about functions vs function-like macros in C. Was reading the C Standard and I found this: Any macro definition of a function can be suppressed locally by enclosing the name of the ...
Cblue X's user avatar
  • 325
3 votes
1 answer
112 views

Julia create multiple slightly modified versions of a function

I have a function that looks like function eom!(du, u, p) @views a, b = u[:,1], u[:,2]; @views da, db = du[:,1], du[:,2]; y = # some stuff involving p and a; da .= f(a, b, y); db ...
Grayscale's user avatar
  • 1,502
3 votes
3 answers
131 views

Is the Macro argument a function?

I am trying to determine whether a given argument within a macro is a function, something like (defmacro call-special? [a b] (if (ifn? a) `(~a ~b) `(-> ~b ~a))) So that the following ...
Stephen Cagle's user avatar
3 votes
3 answers
2k views

Creating Inline Functions and Macros

I know this is wrong (trying to code in C# with a C/C++ mindset). But is it possible to create inline functions / inline recursive functions (til the Nth call) / macros in C#? Because if I intend to ...
Trauer's user avatar
  • 1,999
3 votes
1 answer
126 views

Get Name Current Function

Is there a way to get the name of the current function in D? I'm looking for something similar to __FUNCTION__ in C++. I would like to use this name to pass it into an Exception. The Exception would ...
Jeroen's user avatar
  • 15.9k
3 votes
1 answer
76 views

How to have a function argument as a parameter to a macro in C

I want to pass an argument that is a function into a macro, and it's obvious from compiler message errors I'm not doing it right: #define mu_run(func()) func() == 0 ? printf_green("Test passed: %...
Aaron Barkat-Masih's user avatar
3 votes
1 answer
178 views

Predefining functions that are automatically generated with Common Lisp macros

Macros may create functions in the global scope. For example: (defmacro test-macro (&body functions) `(progn ,@(loop for function in functions collect `(defun ,function () ...
ElliotPenson's user avatar
3 votes
4 answers
3k views

How to bind F1-F12 key as keyboard macro in Emacs?

I want to bind F1-F12 key as keyboard macro. Starting kbd-macro with Shift-[F1-F12] and execute it with [F1-F12]. Each key can have different macro. Is there any way to do this?
Kei Minagawa's user avatar
  • 4,445
3 votes
5 answers
8k views

How to convert a variable argument function into a macro?

I have a variable argument function which prints error messages in my application, whose code is given below: void error(char *format,...) { va_list args; printf("Error: "); va_start(args, ...
goldenmean's user avatar
  • 18.7k
3 votes
2 answers
235 views

Where and When should we use compile time macro concatenation?

I've seen this bit of code which I thought was great because it saved me from re-writing getter member functions. #define GET(n_,N_) \ template<typename T> ...
user avatar
3 votes
3 answers
128 views

Is it possible to create named functions in Python similar to C macro?

Sorry for the confusing title. I would like to do the following: (Similar to defstruct in Lisp) def mkstruct(structname, field_dict): # create a function called "structname" and get/set ...
dividebyzero's user avatar
  • 1,253
3 votes
2 answers
2k views

C Unstringification with macros

Is there any way to unstringify strings provided as macro arguments? I need to be able to call functions who's names are in strings. Something like this: void hello() { printf("Hello, world!"); } ...
None's user avatar
  • 3,915
3 votes
0 answers
668 views

Evaluate function macros and expressions with libclang

Is it possible to evaluate expressions and function macros with libclang? Something like this (expression): #define SOMETHING 1 | 2 | 4 | 0x10 Or something with a function macro: #define ADD(a,b) ...
sciencectn's user avatar
  • 1,429

1
2 3 4 5