I don't know if you can compile your program in C++ mode, or if you would like to depend on Boost, but the Boost.Format library combines type safety with convenience.Tom Likens wrote: And yes, before you start throwing stones I know that C++'s "cout << blah blah" syntax would have prevented all this, but frankly I viserally hate that syntax, so to each their own.
Debugging: GCC/Clang __attribute__((format)) Specifier
Moderator: Ras
-
Rein Halbersma
- Posts: 771
- Joined: Tue May 22, 2007 11:13 am
Re: Debugging: GCC/Clang __attribute__((format)) Specifier
-
mvk
- Posts: 589
- Joined: Tue Jun 04, 2013 10:15 pm
Re: Debugging: GCC/Clang __attribute__((format)) Specifier
I doubt these five will be fixed. Anyway, thanks for the "heads up".Tom Likens wrote:Yes, it's amazing how many of these creep in--especially, when compiling for 32-bit vs. 64-bit. I even saw differences between 64-bit Linux and 64-bit Windows. It's was rather humbling. And they can be damaging, when I cleaned this up the 32-bit Windows compile was no longer unstable (i.e. no more crashing).
--tom
[Account deleted]
-
lucasart
- Posts: 3243
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: Debugging: GCC/Clang __attribute__((format)) Specifier
You're kidding right? There's enough uglyness in C++ iostream not to add more with Boost. I thought Tom's initial post was clear about not wanting C++ solutions.Rein Halbersma wrote:I don't know if you can compile your program in C++ mode, or if you would like to depend on Boost, but the Boost.Format library combines type safety with convenience.Tom Likens wrote: And yes, before you start throwing stones I know that C++'s "cout << blah blah" syntax would have prevented all this, but frankly I viserally hate that syntax, so to each their own.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
Rein Halbersma
- Posts: 771
- Joined: Tue May 22, 2007 11:13 am
Re: Debugging: GCC/Clang __attribute__((format)) Specifier
It sounds like you didn't even bother to read the posted link, and only expressed your general feelings about C++...lucasart wrote:You're kidding right? There's enough uglyness in C++ iostream not to add more with Boost. I thought Tom's initial post was clear about not wanting C++ solutions.Rein Halbersma wrote:I don't know if you can compile your program in C++ mode, or if you would like to depend on Boost, but the Boost.Format library combines type safety with convenience.Tom Likens wrote: And yes, before you start throwing stones I know that C++'s "cout << blah blah" syntax would have prevented all this, but frankly I viserally hate that syntax, so to each their own.
What most people hate about iostreams is its extreme verbosity of the various setXXX manipulators, and the fact that formatting options and variables are intertwined. Boost.Format is similar to printf style formatting. Given a formatting string s, the following two lines should do the same
Code: Select all
printf(s, x1, x2);
cout << format(s) ℅ x1 % x2;Only if you are hung up about overloaded operator<< and operator% as a matter of principle, then yes, by all means stick to printf().
-
hMx
- Posts: 61
- Joined: Wed Mar 08, 2006 9:40 pm
- Location: Germany, Berlin
Re: Debugging: GCC/Clang __attribute__((format)) Specifier
To retain some readability in spite of conditional expansion the usual idiom is to use conditionally defined manifests, like this:Tom Likens wrote: Now that I've added the attribute stuff both Clang and gcc catch format vs. argument errors inside all these custom functions. The immediate downside though is that this ties you to those compilers. That's not too tremendous a handicap though, because the prototypes could be #ifdef in and out depending on the compiler.
--tom
Code: Select all
#if defined(__GNUC__)
# define Wprintf12 __attribute__((format (printf, 1, 2)))
#else
# define Wprintf12 /*nothing*/
#endif
void print( const char *format, ...) Wprintf12;
void printL( const char *format, ...) Wprintf12;
-
Tom Likens
- Posts: 303
- Joined: Sat Apr 28, 2012 6:18 pm
- Location: Austin, TX
Re: Debugging: GCC/Clang __attribute__((format)) Specifier
Yes, that was actually my intent. I use this trick all over the place when compiling the release vs. debug version of the engine. As you mention it's much cleaner. Thanks for the tip.hMx wrote:To retain some readability in spite of conditional expansion the usual idiom is to use conditionally defined manifests, like this:Tom Likens wrote: Now that I've added the attribute stuff both Clang and gcc catch format vs. argument errors inside all these custom functions. The immediate downside though is that this ties you to those compilers. That's not too tremendous a handicap though, because the prototypes could be #ifdef in and out depending on the compiler.
--tomIMO that is much better than using #if or #ifdef at each usage point. And you get the additional checking wherever you have support for it. On all other platforms you just have no further checking, but still the same code.Code: Select all
#if defined(__GNUC__) # define Wprintf12 __attribute__((format (printf, 1, 2))) #else # define Wprintf12 /*nothing*/ #endif void print( const char *format, ...) Wprintf12; void printL( const char *format, ...) Wprintf12;
--tom