Questions tagged [macros]

***DO NOT USE for VBA / MS-Office languages. Use [vba] instead.*** A macro is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to an output sequence (also often a sequence of characters) according to a defined procedure.

macros
Filter by
Sorted by
Tagged with
32 votes
4 answers
3k views

Generalized Threading Macro in Clojure

Note: this is NOT about concurrency. This is about the thread macro. I know that -> puts the object at the 2nd position and ->> puts the argument at the last position. Now, I'm curious, ...
user avatar
32 votes
11 answers
11k views

Easy way to convert exec sp_executesql to a normal query?

When dealing with debugging queries using Profiler and SSMS, its pretty common for me to copy a query from Profiler and test them in SSMS. Because I use parameterized sql, my queries are all sent as ...
user avatar
32 votes
8 answers
13k views

Macro for making numbered lists in vim?

Often times it seems I have a list of items, and I need to add numbers in front of them. For example: Item one Item two Item three Which should be: 1. Item one 2. Item two 3. Item three In vim, I ...
Nik Reiman's user avatar
  • 39.7k
32 votes
6 answers
40k views

How big can a malloc be in C?

I have a malloc in C that is 26901^2*sizeof(double) This got me thinking what the largest value can be here? Also, would I have any problems defining a macro to access this 2D array? #define DN(i,...
Derek's user avatar
  • 11.8k
32 votes
4 answers
32k views

when to use JNIEXPORT and JNICALL in Android NDK?

I'm trying to write my own jni sources. Looking at some ndk samples, I found that they often use those macros JNIEXPORT and JNICALL follewed by the name of java package like this JNIEXPORT void ...
nawara's user avatar
  • 1,167
32 votes
3 answers
3k views

Auto release of stack variables in C

Unfortunately, in C there aren't any smart pointers.. but is it possible to build a macro that wrap variable declaration and invoke function call with that variable as an input variable upon leaving ...
Irad K's user avatar
  • 877
32 votes
2 answers
20k views

Expand macro inside string literal

What I'm trying to do is to #define a macro: #define a(2) and later use it inside a string literal: string = "a";. I want that string to be interpreted not as string but to get the value of a, i.e. ...
Amine's user avatar
  • 347
32 votes
4 answers
10k views

Why is this C or C++ macro not expanded by the preprocessor?

Can someone points me the problem in the code when compiled with gcc 4.1.0. #define X 10 int main() { double a = 1e-X; return 0; } I am getting error:Exponent has no digits. When I replace X ...
Atul 's user avatar
  • 1,126
32 votes
7 answers
40k views

Is it possible for C preprocessor macros to contain preprocessor directives?

I would like to do the equivalent of the following: #define print_max(TYPE) \ # ifdef TYPE##_MAX \ printf("%lld\n", TYPE##_MAX); \ # endif print_max(INT); Now the #ifdef or any nested ...
pixelbeat's user avatar
  • 31.1k
31 votes
8 answers
36k views

How can I simulate macros in JavaScript?

I know that JavaScript doesn't support macros (Lisp-style ones) but I was wondering if anyone had a solution to maybe simulate macros? I Googled it, and one of the solutions suggested using eval(), ...
Anders Rune Jensen's user avatar
31 votes
2 answers
73k views

Redefining or changing macro value

I am currently working on an already developed project written in MFC C++ and am facing a problem with an already present macro having the definition: #define HEIGHT_TESTS 13 I am trying to change ...
Neophile's user avatar
  • 5,770
31 votes
5 answers
87k views

Is max(a,b) defined in stdlib.h or not?

I'm using two computers, each with a different version of visual studio. On the visual studio 2008 computer my code compiles. On the visual 2010 computer my code doesn't compile because I'm using the ...
snakile's user avatar
  • 53.8k
31 votes
3 answers
14k views

Are empty macro arguments legal in C++11?

I sometimes deliberately omit macro arguments. For example, for a function-like macro like #define MY_MACRO(A, B, C) ... I might call it as: MY_MACRO(, bar, baz) There are still technically 3 ...
SuperElectric's user avatar
31 votes
3 answers
31k views

How to use Objective-C code with #define macros in Swift

I'm trying to use a third-party Objective-C library in a Swift project of mine. I have the library successfully imported into Xcode, and I've made a <Project>-Bridging-Header.h file that's ...
ankushg's user avatar
  • 1,632
31 votes
5 answers
7k views

Using $crate in Rust's procedural macros?

I know what the $crate variable is, but as far as I can tell, it can't be used inside procedural macros. Is there another way to achieve a similar effect? I have an example that roughly requires me ...
Daniel Fath's user avatar
  • 17.3k
30 votes
8 answers
38k views

How to generate random variable names in C++ using macros?

I'm creating a macro in C++ that declares a variable and assigns some value to it. Depending on how the macro is used, the second occurrence of the macro can override the value of the first variable. ...
freitass's user avatar
  • 6,612
30 votes
8 answers
109k views

Is it possible to use a if statement inside #define?

I'm trying to make a macro with the following formula: (a^2/(a+b))*b, and I want to make sure that the there will be no dividing by zero. #define SUM_A( x, y ) if( x == 0 || y == 0) { 0 } else { ( ( ...
Frey1337's user avatar
  • 433
30 votes
2 answers
3k views

Is it wrong to add preprocessor directives in a function-like macro?

I know that my question is similar to this one or this one, but I find that it is not really the same and, more, the second one has not an answer accepted, I decided to ask if it is correct to add ...
sop's user avatar
  • 3,515
30 votes
7 answers
31k views

Instantiate class from name?

imagine I have a bunch of C++ related classes (all extending the same base class and providing the same constructor) that I declared in a common header file (which I include), and their ...
puccio's user avatar
  • 3,074
30 votes
3 answers
36k views

Custom C++ assert macro

I stumbled upon an informative article: http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ which pointed out a great number of problems that exist in my current suite of debugging ...
Steven Lu's user avatar
  • 42.3k
30 votes
6 answers
6k views

Can someone explain the concept of 'hygiene' to me (I'm a scheme programmer)?

So... I'm new to scheme r6rs, and am learning macros. Can somebody explain to me what is meant by 'hygiene'? Thanks in advance.
Cam's user avatar
  • 15.1k
30 votes
5 answers
6k views

Why do I need double layer of indirection for macros?

At: C++ FAQ - Miscellaneous technical issues - [39.6] What should be done with macros that need to paste two tokens together? Could someone explain to me why? All I read is trust me, but I simply ...
Rookie's user avatar
  • 4,104
30 votes
1 answer
10k views

GCC define function-like macros using -D argument

Problem I am trying to remove __attribute__ from my C code before I send it into a parser. Is there a way to define function-like macros using the -D argument? Solution using header file #define ...
Stefan Bossbaly's user avatar
29 votes
6 answers
6k views

Good examples of Clojure macros usage which demonstrate advantages of the language over the mainstream?

I am thinking about learning Clojure, but coming from the c-syntax based (java, php, c#) world of imperative languages that's going to be a challenge, so one naturally asks oneself, is it really worth ...
Cray's user avatar
  • 2,416
29 votes
5 answers
31k views

Determine #defined string length at compile time

I have a C-program (an Apache module, i.e. the program runs often), which is going to write() a 0-terminated string over a socket, so I need to know its length. The string is #defined as: #define ...
Alexander Farber's user avatar
29 votes
5 answers
26k views

Is #if defined MACRO equivalent to #ifdef MACRO?

I have code that I want to have two modes, debug and verbose. I define them in my header file as, #define verbose TRUE #define debug TRUE In my code so far, I have just been using #if(debug) //code ...
msc's user avatar
  • 33.9k
29 votes
9 answers
3k views

What is the best way to do loops in JavaScript

I have stumbled into several methods of looping in JavaScript, what I like the most is: for(var i = 0; i < a.length; i++){ var element = a[i]; } But as tested here (http://www.robertnyman.com/...
Anders Rune Jensen's user avatar
29 votes
3 answers
2k views

When did the idea of macros (user-defined code transformation) appear?

I have read McCarthy's 1960 paper on LISP and found no reference to anything that's similar to user-defined macros or normal order evaluation. I was wondering when macros first appeared in programming ...
Jay's user avatar
  • 9,586
29 votes
5 answers
30k views

How to run python macros in LibreOffice?

When I go to Tools -> Macros -> Organize Macros -> Python I get this dialog: It is not possible to create new Python macros. Apparently LibreOffice has no Python editor so I have to write the macros ...
Martin Drozdik's user avatar
29 votes
1 answer
3k views

Matching function literals with quasiquotes in Scala

This question is similar in motivation to my previous question (although it's about a problem I ran into in a different context). I can pattern match on a function literal pretty easily without ...
Travis Brown's user avatar
28 votes
8 answers
39k views

Creating a string list and an enum list from a C++ macro

In order to make my code shorter and easier to change I want to replace something like enum{ E_AAA, E_BBB, E_CCC }; static const char *strings{"AAA", "BBB", "CCC" }; With a macro, like INIT(AAA, BBB,...
Tiago's user avatar
  • 271
28 votes
4 answers
112k views

inline function vs macro function [duplicate]

Possible Duplicate: Inline functions vs Preprocessor macros I want to know the difference between the inline function and macro function. 1) is inline function is the same of macro function ? 2)...
MOHAMED's user avatar
  • 42.6k
28 votes
8 answers
19k views

Trouble with template parameters used in macros

I'm trying to compile the following piece of code, I get an error on the line which specializes std::vector, it seems the one parameter being passed-in is somehow being assumed to be two parameters. ...
user avatar
28 votes
4 answers
2k views

How to allow copy elision construction for C++ classes (not just POD C structs)

Consider the following code: #include <iostream> #include <type_traits> struct A { A() {} A(const A&) { std::cout << "Copy" << std::endl; } A(A&&) { std::...
Clinton's user avatar
  • 22.8k
27 votes
9 answers
86k views

Have macro 'return' a value

I'm using a macro and I think it works fine - #define CStrNullLastNL(str) {char* nl=strrchr(str,'\n'); if(nl){*nl=0;}} So it works to zero out the last newline in a string, really its used to chop ...
bobobobo's user avatar
  • 66.1k
27 votes
3 answers
7k views

In Clojure, when should we use a monad instead of a macro and vice-versa?

There are too many tutorials out there on monads that say... "Look! here is a case where we can use a monad" or "This is what a monad is for". What I want to know is what are some of the steps that ...
zcaudate's user avatar
  • 14.1k
27 votes
7 answers
28k views

What is the difference between a macro and a const in C++?

I was asked this question in a technical interview: What is the difference between a const and a macro in C++? My answer was that a macro is a preprocessor directive and it could be difficult to ...
Amm Sokun's user avatar
  • 1,298
27 votes
3 answers
41k views

What exactly do C include guards do?

Let's say I have a header file "header.h" with a function definition. #ifndef HEADER_FILE #define HEADER_FILE int two(void){ return 2; } #endif This header file has an include guard. ...
Izzo's user avatar
  • 4,649
27 votes
3 answers
6k views

What are the advantages of scheme macros?

Why would anyone prefer Scheme macros over Common Lisp macros (and I genuinely want to know too, I'm not trying to be a troll)? My experience as a Lisp newb is that Common Lisp style macros are much ...
Jason Baker's user avatar
27 votes
3 answers
1k views

Why this macro is defined as ({ 1; })?

In multiple ARM back-end of Linux, I'm seeing in files clkdev.h this macro definition: #define __clk_get(clk) ({ 1; }) See for example ./arch/arm/mach-versatile/include/mach/clkdev.h This macro is ...
Yann Droneaud's user avatar
27 votes
1 answer
4k views

Why is a level of indirection needed for this concatenation macro?

I found an interesting little blog post that explains how to generate (semi) unique names in a macro by using the line number: // Do magic! Creates a unique name using the line number #define ...
DaoWen's user avatar
  • 32.8k
26 votes
12 answers
4k views

Rare cases where MACROs must be used

Debugging macros can take a lot of time. We are much better off avoiding them except in the very rare cases when neither constants, functions nor templates can do what we want. What are the rare ...
Terry Li's user avatar
  • 17.1k
26 votes
6 answers
18k views

What is the Swift preprocessor equivalent to iOS version check comparison?

I am getting yld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings and here's the function that is causing the error when the application is running on an iOS7 device and without even ...
Essa A. Haddad's user avatar
26 votes
13 answers
14k views

Can I determine if an argument is string literal?

Is it possible to determine if an argument passed in macro or function is a string literal at compile time or run time? For example, #define is_string_literal(X) ... ... is_string_literal("...
absurd's user avatar
  • 301
26 votes
7 answers
6k views

How to find out cl.exe's built-in macros

Does anyone know how could I find out which are cl.exe's builtin/predefined macros? For example for gcc the following command line will list all the compiler's builtin macros gcc -dM -E - </dev/...
celavek's user avatar
  • 5,635
26 votes
3 answers
13k views

Macro evaluation order [duplicate]

Possible Duplicate: # and ## in macros why the output of second printf is f(1,2) what is the order in which macro is evaluated? #include <stdio.h> #define f(a,b) a##b #define g(a) #a #...
Utkarsh Srivastav's user avatar
26 votes
2 answers
4k views

What is the purpose of ~' or '~ in Clojure?

I am learning about Clojure macros, and the code examples will sometimes have the constructs '~symbol or alternately ~'symbol. I know that (quote and ' prevent a form from being evaluated, and that ...
Alex's user avatar
  • 325
26 votes
2 answers
8k views

Given the following LISP eval function - what is required to add defmacro?

Given the following definition of the LISP eval function - what is required to add the defmacro function? (Or even just evaluate a macro) (defun null. (x) (eq x '())) (defun and. (x y) (cond ...
hawkeye's user avatar
  • 35.2k
26 votes
2 answers
40k views

Documenting Macro Functions in C++ with Doxygen

How do I document a macro function in C++ using Doxygen, and refer to it in the documentation of my non-Evil code? More specifically, I have some regular class called "Message" defined in Message.H ...
rcv's user avatar
  • 6,168
25 votes
4 answers
7k views

pass method with template arguments to a macro

I am unable to use Google Test's ASSERT_THROW() macro in combination with multiple template arguments. Consider that I want to make sure that construction of Matrix<5,1> throws: ASSERT_THROW(...
Philipp's user avatar
  • 11.7k