All Questions
227
questions
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 ...
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)
...
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 ...
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 + ...
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?
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) (...
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--) {
...
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 ...
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 ...
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 ...
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 ...
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 ...
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)))
`(...
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 ...
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 ...
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;
}
...
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()...
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)...
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?
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)...
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 ...
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?
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 ...
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) ...
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 ...
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()...
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 ...
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 ...
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 ...
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 <...
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 ...
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:...
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");...
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 ...
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 ...
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 ...
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}...
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 ...
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 ...
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 ...
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 ...
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 ...
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: %...
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 ()
...
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?
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, ...
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> ...
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 ...
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!");
}
...
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) ...