How to get printf style compile-time warnings or errors

后端 未结 5 1680
小蘑菇
小蘑菇 2020-12-14 10:39

I would like to write a routine like printf, not functionally-wise, but rather I\'d like the routine to have the same time compile check characteristics as printf.

F

相关标签:
5条回答
  • 2020-12-14 11:22

    A while back someone posted an mpl::string to the boost groups. I think it may actually have gotten into the library. If that is the case you could implement something like this by providing your template string as a template parameter (an mpl::string) and then using some pretty profound meta-programming skills to parse the formatting bits in it. Then you'd use this information to chose an implementation that has the appropriate argument count and types.

    No, I'm not going to do it for you :P It would be quite difficult. However, I do believe it would be possible.

    0 讨论(0)
  • 2020-12-14 11:28

    Different compilers might implement this functionality differently. In GCC it is implemented through __attribute__ specifier with format attribute (read about it here). The reason why the compiler performs the checking is just that in the standard header files supplied with GCC the printf function is declared with __attribute__((format(printf, 1, 2)))

    In exactly the same way you can use format attribute to extend the same format-checking functionality to your own variadic functions that use the same format specifiers as printf.

    This all will only work if the parameter passing convention and the format specifiers you use are the same as the ones used by the standard printf and scanf functions. The checks are hardcoded into the compiler. If you are using a different convention for variadic argument passing, the compiler will not help you to check it.

    0 讨论(0)
  • 2020-12-14 11:38

    Actually printf doesn't have any inherent compile-time safety at all. It just happens that some more recent compilers have implemented special checks given that they know exactly what a format string means in terms of the additional parameters. When you use ... as a parameter you're saying that you want to accept arbitrary arguments and accept full responsibility for making sure they're correct. There's no way for the compiler to check them for count/type safety.

    Instead of trying to get the compiler to help you in this way, try using the approach used by standard streams: Make use of a (possibly template) function or operator that returns a reference to this to allow chaining. Then the compiler will be able to tell you right away when the arguments don't match what's expected/supported.

    0 讨论(0)
  • 2020-12-14 11:39

    This behavior is highly compiler dependent. I believe that gcc provides an interface for type checking variadic functions.

    0 讨论(0)
  • 2020-12-14 11:44

    printf() and friends aren't special because they accept a variable number of arguments: user-defined functions can accept a variable number of arguments too. They are special because their behavior is defined by standard, so the compiler knows what the correlation should be between the format string and the arguments passed to the function.

    Effectively, the compiler knows how many arguments are passed to the function when it is called, so it parses the format string and compares the expected number and types of arguments against what arguments are actually passed to the function and issues a warning if they don't match.

    If you are using C++, I would avoid writing your own variadic functions; there are few good reasons to use them in most projects. For example, if you are doing formatting, use streams or a library like Boost Format. Any problem that can be solved using a variadic function can be solved using a non-variadic function, and in almost all cases the result is more elegant, idiomatic, and type-safe.

    0 讨论(0)
提交回复
热议问题