I have defined a macro as following:
#define ADD_TIME_ENTRY(_name_) m_pTimeMeasurement->addTimeEntry(_name_);
Now, I want to pass the function name through ADD_TIME_ENTRY() in whichsoever function I put ADD_TIME_ENTRY()
ADD_TIME_ENTRY(__FUNCTION__) works fine for me but now, I want to add "_start" or "_stop" at the end of the function name. I mean, I need something like "imageFunction()_start" but I am not able to do it.
I want to do it in a single line i.e.
imageFunction()
{
ADD_TIME_ENTRY(__FUNCTION__ ....something here...);
}
You can add an additional macro #define STRINGIZE(str) #str and use it like this: ADD_TIME_ENTRY(__FUNCTION__ STRINGIZE(start))
Seems like __FUNCTION__ might be not a macro so it is not particularly easy to combine it with anything in such a case. In MSVC2013 it is a macro so it is easy combinable. For other compilers I'd try the following:
#define GIVE_ME_NAME(tag) std::string(__FUNCTION__) + STRINGIZE(tag)
Not particularly efficient but working way.
Example:
#include <iostream>
#include <string>
using namespace std;
#define STRINGIZE(str) #str
#define GIVE_ME_NAME(tag) std::string(__FUNCTION__) + STRINGIZE(tag)
int main()
{
std::cout << GIVE_ME_NAME(start);
};
As __FUNCTION__ is of type static "const char __FUNCTION__[]" , when you call m_pTimeMeasurement->addTimeEntry(_name_),addTimeEntry function parameter will be "const char __FUNCTION__[]".
So you can concatenate two char variables and pass it
ADD_TIME_ENTRY(strcat(const_cast< char *>(__FUNCTION__),"_start"));
ADD_TIME_ENTRY(strcat(const_cast<char *>(__FUNCTION__),"_stop"));
Related
I need to use #define and using = ; as much as I can to replace possibly everything in C++ with emojis 😝😜😫😻😋.
Is it possible to #define preprocessors like #define 😎 #define or at least #define 😖 if, #define 🍉 ==, etc.? Maybe with 'using'?
I'd like to replace operators, core language instructions... Is it possible anyhow?
I know the aboves doesn't work, but maybe there is a way?... Please help me make something funny! :D
Yes, you can. You may need to think about the syntax. Easiest would be to use one emoji per keyword. However you may still need to write function- and variable names in clear text.
As per Romens comment I tried it and you can also replace method names with emojis.
Just as a proof of concept, the following code compiles in visual studio 2019 with platform toolset v142.
#include <iostream>
#define 😎 int
😎 🍉() {
std::cout << "I'm 🍉!";
return 1;
}
😎 main() {
🍉();
}
Or even more to include some of the comments:
#include <iostream>
#define 🙈 using
#define 🤷🏻 cout
#define 😎 int
namespace 🍏 = std;
🙈 🍏::🤷🏻;
😎 🍉() {
🤷🏻 << "I'm";
🍏::cout << "🍉!";
return 1;
}
😎 main() {
🍉();
}
Also using is something else than #define. You will only need the latter.
Is it possible to #define preprocessors like #define 😎 #define
No, it is not possible to define macros to replace preprocessor directives. (Also, macros cannot expand into directives either).
or at least #define 😖 if
This is potentially possible. It depends on the compiler what input character encoding it supports. Emojis are not listed in the basic source character set specified by the language standard, so they might not exist in the character encoding used by the compiler.
Maybe with 'using'?
Emojis are equally allowed for using as they are for macros.
Note that any identifier could be an emoji (assuming they are supported in the first place) including functions, types and variables. Example:
struct 🍏🍏 {};
struct 🍊🍊 {};
int main() {
🍏🍏{} == 🍊🍊{};
}
I'm writing a bunch of related preprocessor macros, one of which generates labels which the other one jumps to. I use them in this fashion:
MAKE_FUNNY_JUMPING_LOOP(
MAKE_LABEL();
MAKE_LABEL();
)
I need some way to generate unique labels, one for each inner MAKE_LABEL call, with the preprocessor. I've tried using __LINE__, but since I call MAKE_LABEL inside another macro, they all have the same line and the labels collide.
What I'd like this to expand to is something like:
MAKE_FUNNY_JUMPING_LOOP(
my_cool_label_1: // from first inner macro
...
my_cool_label_2: // from second inner macro
...
)
Is there a way to generate hashes or auto-incrementing integers with the preprocessor?
If you're using GCC or MSVC, there is __COUNTER__.
Other than that, you could do something vomit-worthy, like:
#ifndef USED_1
#define USED_1
1
#else
#ifndef USED_2
#define USED_2
2
/* many many more */
#endif
#endif
I use this:
#define MERGE_(a,b) a##b
#define LABEL_(a) MERGE_(unique_name_, a)
#define UNIQUE_NAME LABEL_(__LINE__)
int main()
{
int UNIQUE_NAME = 1;
return 0;
}
... and get the following:
int main()
{
int unique_name_8 = 1;
return 0;
}
As others noted, __COUNTER__ is the easy but nonstandard way of doing this.
If you need extra portability, or for other cool preprocessor tricks, the Boost Preprocessor library (which works for C as well as C++) will work. For example, the following header file will output a unique label wherever it's included.
#include <boost/preprocessor/arithmetic/inc.hpp>
#include <boost/preprocessor/slot/slot.hpp>
#if !defined(UNIQUE_LABEL)
#define UNIQUE_LABEL
#define BOOST_PP_VALUE 1
#include BOOST_PP_ASSIGN_SLOT(1)
#undef BOOST_PP_VALUE
#else
#define BOOST_PP_VALUE BOOST_PP_INC(BOOST_PP_SLOT(1))
#include BOOST_PP_ASSIGN_SLOT(1)
#undef BOOST_PP_VALUE
#endif
BOOST_PP_CAT(my_cool_label_, BOOST_PP_SLOT(1)):
Sample:
int main(int argc, char *argv[]) {
#include "unique_label.h"
printf("%x\n", 1234);
#include "unique_label.h"
printf("%x\n", 1234);
#include "unique_label.h"
return 0;
}
preprocesses to
int main(int argc, char *argv[]) {
my_cool_label_1:
printf("%x\n", 1234);
my_cool_label_2:
printf("%x\n", 1234);
my_cool_label_3:
return 0;
}
I can't think of a way to automatically generate them but you could pass a parameter to MAKE_LABEL:
#define MAKE_LABEL(n) my_cool_label_##n:
Then...
MAKE_FUNNY_JUMPING_LOOP(
MAKE_LABEL(0);
MAKE_LABEL(1);
)
You could do this:
#define MAKE_LABEL() \
do { \
my_cool_label: \
/* some stuff */; \
goto my_cool_label; \
/* other stuff */; \
} while (0)
This keeps the scope of the label local, allowing any number of them inside the primary macro.
If you want the labels to be accessed more globally, it's not clear how your macro "MAKE_FUNNY_JUMPING_LOOP" references these labels. Can you explain?
It doesn't seem possible with a standard preprocessor, although you could fake it out by putting parameters within MAKE_LABEL or MAKE_FUNNY_JUMPING_LOOP, and use token pasting to create the label.
There's nothing preventing you from making your own preprocessing script that does the automatic increment for you. However, it won't be a standard C/C++ file in that case.
A list of commands available: http://www.cppreference.com/wiki/preprocessor/start
I recently learned of the ## functionality that i can define in the beginning of my code. I'm trying to compile the following code:
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <stdio.h>
#include <string>
#define paste(x,y) *x##*y
int main()
{
TCHAR *pcCommPort = "COM";
TCHAR *num = "5";
cout << paste(pcCommPort,num);
return 0;
}
and i keep getting the following error:
expression must have arithmetic or unscoped enum type
it's not liking the fact that i'm using pointers in my "define paste" line. Without any pointers, it'll just return the variable "pcCommPort5." what I want is "COM5."
I've tried _tcscat, strcat, strcat_s, visual studio didn't like any of these....
## doesn't concatenate arbitrary things (especially not strings). What it does it is merges symbols together in the parser into a single symbol.
Let's remove one of those * to see what's going on:
#include <iostream>
#define TO_STRING_HELPER(x) #x
#define TO_STRING(x) TO_STRING_HELPER(x)
#define CONCAT(x, y) *x##y
int main() {
char *pcCommPort = "COM";
char *num = "5";
std::cout << TO_STRING(CONCAT(pcCommPort, num)) << std::endl;
}
Output:
*pcCommPortnum
What CONCAT does in this code is:
Expand x into pcCommPort and y into num. This gives the expression *pcCommPort##num.
Concatenate the two symbols pcCommPort and num into one new symbol: pcCommPortnum. Now the expression is *pcCommPortnum (remember, that last part (pcCommPortnum) is all one symbol).
Finish evaluating the full macro as a * followed by the symbol pcCommPortnum. This becomes the expression *pcCommPortnum. Remember, those are two different symbols: * and pcCommPortnum. The two symbols just follow one after the other.
If we were to try to use *x##*y, what the compiler does is this:
Expand x into pcCommPort and y into num. This gives us the expression *pcCommPort##*num.
Concatenate the two symbols pcCommPort and * into one new symbol: pcCommPort*.
Here, the preprocessor hits an error: the single symbol pcCommPort* is not a valid preprocessing token. Remember, it's not two separate symbols at this point (it is not two symbols pcCommPort followed by *). It is one single symbol (which we call a token).
If you want to concatenate two strings, you're way better off using std::string. You can't* do what you're trying to do with the preprocessor.
*Note, though, that consecutive string literals will be merged together by the compiler (i.e. "COM" "5" will be merged into a single string "COM5" by the compiler). But this only works with string literals, so you'd have to #define pcCommPort "COM" and #define num "5", at which point you could do pcCommPort num (without any further macros) and the compiler would evaluate it to the string "COM5". But unless you really know what you're doing, you really should just use std::string.
I have, in a .c file, the following (this is a much smaller array to exemplify):
static const char* __someNames[] =
{
"Fox",
"Wulf",
"Cat"
};
Then later I am defining a macro like this:
#define EXAMPLE(N) XXX
where I would like XXX to be replaced by __someNames[N] without the "..i.e I would like
#define EXAMPLE(1)
to expand to:
Wulf
How can I go about this?
EDIT
Some cool ideas. This is more out of intellectual curiosity that I want to do this. I like the idea of the #include "file.h# twice with a redefined macro..what if I can also use the boost preprocessor (I added c++ and boost tags to question) - would I be able to get around having the extra "file.h"? The one with a #define per string is also cool though slightly less idea given how many #defines I would have...
I think the solution to your X problem (where you have asked about the Y solution) is to turn the thing around and use macros to put the string in, and then use the same macro redefined to do whatever else it is you want to do.
For example:
#define EXAMPLE(x) #x,
static const char* __someNames[] =
{
#include "file.h"
};
#undef EXAMPLE
#define EXAMPLE(x) x,
enum animals
{
#include "file.h"
};
where file.h contains:
EXAMPLE(Fox)
EXAMPLE(Wulf)
EXAMPLE(Cat)
There is no way to do this with the standard C or C++ pre-processor.
The pre-processor scans your code for identifiers, and if an identifier matches a macro, it expands the macro. The expanded macro, in this case, would actually be the array name followed by the input number in brackets.
It is a limitation of the way the pre-processor works!
A work-around is like this...
#define EXAMPLE_1 Wolf
#define EXAMPLE_2 Dog
#define EXAMPLE_3 Cat
#define EXAMPLE_X(n) EXAMPLE_##n
// Then say
EXAMPLE_X(1); // will put Wolf;
int EXAMPLE_X(2) = 0;// expands to int Dog = 0;
Unfortunately that only works if you pass in constants like 1 or 2 etc... Variables would not work.
OR ...
static const char* __someNames[] =
{
#define __someNames_1 Fox
"Fox",
#define __someNames_2 Wulf
"Wulf",
#define __someNames_3 Cat
"Cat"
};
#define EXAMPLE_X(n) __someNames_##n
That will work the way you expect and keep the same scheme, but it will put a lot of defines for big arrays!.
#define EXAMPLE(_x) __someNames[(_x)]
I have two macros that declares class properties:
DECLARE_QUERY_PARAM_LONG(name)
DECLARE_QUERY_PARAM_STRING(name)
I want to count number of calls of this macros inside of my class and init
static const size_t paramsCount
with that number like this:
class MyClass {
...
DECLARE_QUERY_PARAM_LONG(param1)
DECLARE_QUERY_PARAM_STRING(param2)
DECLARE_QUERY_PARAM_STRING(param3)
DECLARE_QUERY_PARAM_LONG(param4)
static const size_t paramsCount = PARAMS_COUNT; // 4 in this case
...
};
Is this ever possible?
There would be a solution, rather complicated:
All your parameters will have a fixed name (say param)
You need to create one header file per type
You shall need boost
So here is the header creation file:
// file declare_int.h
#include BOOST_PP_UPDATE_COUNTER()
int stringize(param,BOOST_PP_COUNTER) ;
and the class file:
//file declare_auto.cpp
#include <boost/preprocessor/slot/counter.hpp>
#define _stringize(a,b) a##b
#define stringize(a,b) _stringize(a,b)
// reset counter
#if defined(BOOST_PP_COUNTER)
#undef BOOST_PP_COUNTER
#endif
class A {
public:
#include "declare_int.h"
#include "declare_int.h"
#include "declare_int.h"
#include "declare_int.h"
static const int nbParams = BOOST_PP_COUNTER ;
};
and finally the output of:
g++ -E -P -c declare_auto.cpp -IPATH_TO_BOOST
is
class A {
public:
int param1 ;
int param2 ;
int param3 ;
int param4 ;
static const int nbParams = 4 ;
};
You can at least count number of lines in the following way:
class MyClass
{
static const int line_1 = __LINE__;
DECLARE_QUERY_PARAM_LONG(param1)
DECLARE_QUERY_PARAM_STRING(param2)
DECLARE_QUERY_PARAM_STRING(param3)
DECLARE_QUERY_PARAM_LONG(param4)
static const int line_2 = __LINE__;
static const int macro_calls = line_2 - line_1 - 1;
public:
MyClass()
{
cout << macro_calls << endl;
}
};
But I think you'll need C++11 to do that. And You cannot have empty lines within those two __LINE__s. Otherwise, you'll have to count those empty lines as well.
As such no.
What you are asking for would require some form of introspection, which is not natively supported by C++.
You can improve the macro though, if you had:
DECLARE_QUERY_PARAMS(((LONG , param1))
((STRING, param2))
((STRING, param3))
((LONG , param4)))
then you could do what you want.
You can have a look at Boost.Preprocessor to learn how to obfuscate your sources this way.
Note: this uses a Sequence, in boost parlance.
I don't think there's a standard way to do this, but the DevStudio compiler has this preprocessor macro:
__COUNTER__
Expands to an integer starting with 0 and incrementing by 1 every time it is used in a compiland. __COUNTER__ remembers its state when using precompiled headers. If the last __COUNTER__ value was 4 after building a precompiled header (PCH), it will start with 5 on each PCH use.
__COUNTER__ lets you generate unique variable names. You can use token pasting with a prefix to make a unique name. For example:
// pre_mac_counter.cpp
#include <stdio.h>
#define FUNC2(x,y) x##y
#define FUNC1(x,y) FUNC2(x,y)
#define FUNC(x) FUNC1(x,__COUNTER__)
int FUNC(my_unique_prefix);
int FUNC(my_unique_prefix);
No. Macro's don't respect scope at all, and don't understand that they're inside a class.
No. Macros aren't executed, they're expanded and not even by compiler, but by preprocessor.