在C ++中清除代码printf size_t(或:C ++中最接近C99的%z) [英] Clean code to printf size_t in C++ (or: Nearest equivalent of C99's %z in C++)

查看:596
本文介绍了在C ++中清除代码printf size_t(或:C ++中最接近C99的%z)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些打印 size_t 的C ++代码:

  size_t a; 
printf(%lu,a);

我想在32位和64位架构上编译时不会出现警告。 p>

如果这是C99,我可以使用 printf(%z,a); 。但是在任何标准C ++方言中不存在AFAICT %z 。因此,我必须做

  printf(%lu,(unsigned long)a); 

这真是丑陋。



没有工具打印<​​code> size_t s内置的语言,我想知道是否可能写一个printf包装器或一些,将插入适当的铸造在 size_t ,以便消除伪编译器警告,同时仍然保持良好的编译器警告。



任何想法?





编辑要澄清为什么我使用printf:我有一个相对较大的代码库,我正在清理。它使用printf包装器来执行写警告,将其记录到文件,并且可能退出代码并报错等事情。我可能能够提出足够的C ++ - foo来做这个cout包装,但我不想改变每个warn()调用程序只是为了摆脱一些编译器警告。

解决方案

大多数编译器都有自己的说明符 size_t ptrdiff_t 参数,Visual C ++例如分别使用%Iu和%Id,我认为gcc将允许您使用%zu和%zd。



创建一个宏:

  #if defined(_MSC_VER)||定义(__ MINGW32__)// __ MINGW32__应该__GNUC__ 
的#define JL_SIZE_T_SPECIFIER前行的Iu%
的#define JL_SSIZE_T_SPECIFIER%ID
的#define JL_PTRDIFF_T_SPECIFIER%ID
#elif指令定义(__ GNUC__)
的#define JL_SIZE_T_SPECIFIER%俎
的#define JL_SSIZE_T_SPECIFIERZD%
的#define JL_PTRDIFF_T_SPECIFIERZD%
的#else
// TODO找出哪些使用。
#如果NUMBITS == 32
的#define JL_SIZE_T_SPECIFIER something_unsigned
的#define JL_SSIZE_T_SPECIFIER something_signed
的#define JL_PTRDIFF_T_SPECIFIER something_signed
的#else
的#define JL_SIZE_T_SPECIFIER something_bigger_unsigned
的#define JL_SSIZE_T_SPECIFIER something_bigger_signed
的#define JL_PTRDIFF_T_SPECIFIER东西,bigger_signed
#ENDIF
#ENDIF

用法:

 为size_t一个; 
的printf(JL_SIZE_T_SPECIFIER,A);
的printf(下的大小是JL_SIZE_T_SPECIFIER字节,一);


I have some C++ code that prints a size_t:

size_t a;
printf("%lu", a);

I'd like this to compile without warnings on both 32- and 64-bit architectures.

If this were C99, I could use printf("%z", a);. But AFAICT %z doesn't exist in any standard C++ dialect. So instead, I have to do

printf("%lu", (unsigned long) a);

which is really ugly.

If there's no facility for printing size_ts built into the language, I wonder if it's possible to write a printf wrapper or somesuch such that will insert the appropriate casts on size_ts so as to eliminate spurious compiler warnings while still maintaining the good ones.

Any ideas?


Edit To clarify why I'm using printf: I have a relatively large code base that I'm cleaning up. It uses printf wrappers to do things like "write a warning, log it to a file, and possibly exit the code with an error". I might be able to muster up enough C++-foo to do this with a cout wrapper, but I'd rather not change every warn() call in the program just to get rid of some compiler warnings.

解决方案

Most compilers have their own specifier for size_t and ptrdiff_t arguments, Visual C++ for instance use %Iu and %Id respectively, I think that gcc will allow you to use %zu and %zd.

You could create a macro:

#if defined(_MSC_VER) || defined(__MINGW32__) //__MINGW32__ should goes before __GNUC__
  #define JL_SIZE_T_SPECIFIER    "%Iu"
  #define JL_SSIZE_T_SPECIFIER   "%Id"
  #define JL_PTRDIFF_T_SPECIFIER "%Id"
#elif defined(__GNUC__)
  #define JL_SIZE_T_SPECIFIER    "%zu"
  #define JL_SSIZE_T_SPECIFIER   "%zd"
  #define JL_PTRDIFF_T_SPECIFIER "%zd"
#else
  // TODO figure out which to use.
  #if NUMBITS == 32
    #define JL_SIZE_T_SPECIFIER    something_unsigned
    #define JL_SSIZE_T_SPECIFIER   something_signed
    #define JL_PTRDIFF_T_SPECIFIER something_signed
  #else
    #define JL_SIZE_T_SPECIFIER    something_bigger_unsigned
    #define JL_SSIZE_T_SPECIFIER   something_bigger_signed
    #define JL_PTRDIFF_T_SPECIFIER something-bigger_signed
  #endif
#endif

Usage:

size_t a;
printf(JL_SIZE_T_SPECIFIER, a);
printf("The size of a is " JL_SIZE_T_SPECIFIER " bytes", a);

这篇关于在C ++中清除代码printf size_t(或:C ++中最接近C99的%z)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆