Is this code snippet portable?
definition:
#define log(...) std::cout << __FILE__ << "[" << __LINE__ << "]:" \
<< string_format(__VA_ARGS__) \
<< std::endl << std::flush
usage:
log("i = %d", i);
and string_format is sprintf that outputs a std::string taken from https://stackoverflow.com/a/8098080/624074 It works with my gcc 4.6.3 here but I don't want to have compilation issues later with other compilers.
Yes, the macro __VA_ARGS__ is part of standard C++:
C++11 §16.3.1 Argument substitution
An identifier __VA_ARGS__that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing tokens used to replace it.
It's pretty portable, for the most part. It's part of the C++11 language standard (as well as C99), so most modern compilers support it. You might run into problems with older compilers that only support C++03.
Related
Running the following code
#include <iostream>
#define FOO
#define BAR defined(FOO)
int main() {
#if BAR
std::cout << "BAR enabled!" << std::endl;
#else
std::cout << "BAR disabled!" << std::endl;
#endif
return 0;
}
in Visual Studio displays Bar disabled!, while running the same code in gcc or clang displays Bar enabled!.
Is this a bug in the Microsoft compiler? What is correct according to the standard?
This is undefined behavior according to the standard.
[cpp.cond], emphasis mine
Prior to evaluation, macro invocations in the list of preprocessing tokens that will become the controlling constant expression are replaced (except for those macro names modified by the defined unary operator), just as in normal text. If the token defined is generated as a result of this replacement process or use of the defined unary operator does not match one of the two specified forms prior to macro replacement, the behavior is undefined.
Say I have a C macro-defined product version like this:
#define FOO_VERSION 1,0,0,1
And I want to print it at run-time:
#define STRING_VALUE(x) #x
int main()
{
std::cout << STRING_VALUE(FOO_VERSION) << std::endl;
}
This will output a string "FOO_VERSION", not "1,0,0,1". The macro
argument 'FOO_VERSION' is not replaced. So I try it again like
this:
#define STRING_VALUE(x) STRING_VALUE__(x)
#define STRING_VALUE__(x) #x
int main()
{
std::cout << STRING_VALUE(FOO_VERSION) << std::endl;
}
It works, in Visual C++ 2013.
This is a cross-platform application although there are only five
lines of code. When I use clang to compile the code, a compile-
time error appears: "too many arguments provided to function-like
macro invocation". I guess the reason is the comma defined in
'FOO_VERSION'. So the third version:
#define STRING_VALUE(x) STRING_VALUE__(x)
#define STRING_VALUE__(a, b, c, d) #a "," #b "," #c "," #d
int main()
{
std::cout << STRING_VALUE(FOO_VERSION) << std::endl;
}
This code works in clang, but Visual C++ will output a warning:
"not enough actual parameters for macro 'STRING_VALUE__'" at
compile-time, of course the run-time output is not right.
My question: is this pre-processor behavior defined ? Can I have a
universal version of STRING_VALUE macro ?
You can treat the argument as a single variadic macro:
#define FOO_VERSION 1,0,0,1
#define STRING_VALUE(...) STRING_VALUE__(__VA_ARGS__)
#define STRING_VALUE__(...) #__VA_ARGS__
This seems to work with gcc and Visual C++.
Given the following code:
1. #include <iostream>
2. #define CALL_FUNC(f) f();
3.
4. int main()
5. {
6. CALL_FUNC([](){
7. std::cout << "I'm on line " << __LINE__ << std::endl;
8. });
9. return 0;
10. }
When I compile this in Visual Studio 2012 and g++ 4.7.3, I get the output "I'm on line 8".
When I compile this in clang, I get the output "I'm on line 7", which is what I expected.
Does anyone know which of these is the correct behavior? Is there a way to get the desired behavior in VS and g++ while still having a CALL_FUNC macro?
The standard does not specify the interaction between macro expansion and the __LINE__ predefined macro. In particular a macro invocation that spans multiple lines and contains a __LINE__ token exhibits different behaviours on different preprocessors. When part of a macro argument that is expanded, some will give it the line number of the enclosing macro head name (6), some the closing parenthesis (8) and some the __LINE__ token line (7). It depends on the algorithm used, and there are several that are valid and standard-conformant.
This can be demonstrated as follows:
#define F(x) x
F(
__LINE__
)
Some preprocessors will output 2, some 3 and some 4.
A quick test seems to show that if you move the }); up a line it works:
std::cout << "I'm on line " << __LINE__ << std::endl;});
gcc and msvc seem to think it is on the next line because of the }); on the next line.
(Also, you have an extra semi-colon BTW).
__FILE__ and __LINE__ are well known. There is a __func__ since C99.
#include <iostream>
struct Foo {
void Do(){ std::cout << __func__ << std::endl; }
};
int main()
{
std::cout << __func__ << std::endl;
Foo foo; foo.Do();
return 0;
}
will output
main
Do
Is there any macro / keyword that would output method name like Foo::Do?
Boost has a special utility macro called BOOST_CURRENT_FUNCTION that hides the differences between the compiler implementations.
Following it's implementation we see that there are several macros depending on compiler:
__PRETTY_FUNCTION__ -- GCC, MetroWerks, Digital Mars, ICC, MinGW
__FUNCSIG__ -- MSVC
__FUNCTION__ -- Intel and IBM
__FUNC__ -- Borland
__func__ -- ANSI C99
On GCC you can use __FUNCTION__ and
__PRETTY_FUNCTION__.
On MSVC you can use __FUNCSIG__ and __FUNCTION__.
There's no such macro in standard C++, and that includes the draft C++0x standard I looked at. It would complicate compilation, since parsing (necessary to determine what a function is) comes after preprocessing, and I suspect that's why there's nothing in the standard.
The __func__ you're using is nonstandard, although it apparently works on your compiler.
Not in Standard C++ (and __func__ is not part of C++). Your implementation may have such a feature though - which compiler are you using?
See "Predefined Macros (C/C++)" for a complete list supported by MS Visual Studio.
This might be useful:
http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
#include"stdio.h"
#include"stdlib.h"
int main()
{
printf("line number is %d .. func name is %s, file name is %s",__LINE__,__PRETTY_FUNCTION__,__FILE__);
return 0;
}
This is how to print line number, function name and file name in gcc.
I am trying to figure out how people use the __LINE__ macro at work. How do you guys use it?
You can use __LINE__, along with __FILE__, to report where problems occur in the source code.
Consider, for example, the assert() macro - this wording from the C99 standard, but C++ is similar, except it does not mention __func__:
When it is executed, if expression (which shall have a scalar type) is false (that is,
compares equal to 0), the assert macro writes information about the particular call that
failed (including the text of the argument, the name of the source file, the source line
number, and the name of the enclosing function — the latter are respectively the values of
the preprocessing macros __FILE__ and __LINE__ and of the identifier
__func__) on the standard error stream in an implementation-defined format.
#define assert(x) (void)(((x)==0)?_Assert(#x,__FILE__,__LINE__,__func__):0)
An even simpler example, here's a macro I occasionally use when all other forms of debugging have failed:
#define GOT_HERE std::cout << "At " __FILE__ ":" << __LINE__ << std::endl
Then when your code is mysteriously crashing and taking the device down with it, you can at least sprinkle this macro liberally into the source, in the hope that it will let you narrow down where the crash occurs (and after how many repeats). In practice you might not write to std::cout, but rather use the lowest-level, most reliable character output available on the platform.
I wrote a wrapper for malloc:
#if DEBUG
#define malloc(s) debugging_malloc(s, __FILE__, __LINE__)
#define free(p) debugging_free(p)
// Also, calloc(), strdup(), realloc() should be wrapped.
#endif // DEBUG
Within the wrapper, allocations are tracked according to the file and line number at which they occur. When the program exits, a utility functions outputs a list of any unfreed blocks.
If you are using gcc, it also gives you __FUNCTION__, which is really nice. C99-compliant compilers also have __func__, just as handy.
Most of the answers so far have involved some sort of debugging code. Another use of __LINE__ (and possibly __FILE__) is to enable a macro to generate a unique identifier each time it's called. This implementation of coroutines in C uses these macros to generate labels for a case statement, so it can jump to the last line you called yield from.
Just like this:
$ cat line.cc
#include <iostream>
int main(void) {
std::cout << "Hello, world from " << __LINE__ << std::endl;
return 0;
}
$ g++ -o line line.cc
$ ./line
Hello, world from 4
$
if (something truly impossible has happened) {
fprintf(stderr, "Value of PI less than 3 detected at %s line %d\n", __FILE__, __LINE__);
}
It's especially handy in CPP macros that detect intolerable conditions.
It's really just for debugging/logging purposes. I rarely use it because I tend to rely on debuggers like gdb more often.
But you could always use it when outputting debug information.
void dosomething(Object* o)
{
if (!o) {
std::cerr << __FILE__ << ":" << __LINE__ << ": o is NULL!\n";
abort();
}
}
Just to vaguely quote from the top of my head:
#define assert(e) ((e) ? (void)0 : printf("Assertion failed %s, %s : %s", #e, __FILE__, __LINE__))