简体   繁体   中英

Using std::chrono::duration::rep with printf in 32bit and 64bit programs

There is this code:

#include <cstdio>
#include <chrono>

int main()
{
  auto d = std::chrono::microseconds(1).count();
  printf("%lld", d);
  return 0;
}

When this is compiled in 64bit mode, then there is a warning:

 main.cpp: In function 'int main()': main.cpp:7:19: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'long int' [-Wformat=] printf("%lld", d); ^ 

This warning is not present when compiling in 32bit mode (with -m32 flag). It looks like that std::chrono::duration::rep is of type long int in 64bit programs and long long int in 32bit programs.

Is there a portable way to print it like %zu specifier for size_t ?

As you said that the usage of std::cout is not an option you can cast the value to the smallest needed data type 1 here it's long long int 2 and use the corresponding conversion specifier:

printf("%lld", static_cast<long long int>(d));

To avoid the explicit cast you can also use the data type directly instead of the auto specifier :

long long int d = std::chrono::microseconds(1).count();
printf("%lld", d);

1 With smallest needed data type I mean the smallest type that can represent the value in both implementations.
2 The long long int type has to be at least 64 bit wide, see here on SO .

I suggest you use std::cout , since you are in C++. This will be portable.


However, if you must use printf, change this:

printf("%lld", d);

to this:

#include <cinttypes>
printf("%" PRId64 "", d); 

Another approach would be to cast d to the highest data type (which can hold both types), like this:

printf("%lld", static_cast<long long int>(d));

You can cast it to long long int before printing:

#include <cstdio>
#include <chrono>

int main()
{
  auto d = std::chrono::microseconds(1).count();
  printf("%lld", static_cast<long long int>(d));
  return 0;
}

But it seems me that it is better to use std::cout

Instead of using the auto qualifier, use a fixed size integer int64_t.

#include <cstdio>
#include <chrono>
#include <cinttypes>

int main()
{
    int64_t d = std::chrono::microseconds(1).count();
    printf("%" PRId64 "\n", d);
    return 0;
}

A portable (ie C++) approach to consider, that does not use std::cout

 {
   // create a string:
   std::ostringstream ss; 
   ss << d;" 
   // then
   printf("%s", ss.str().c_str());
 } 

or perhaps

 {
   printf("%s", std::to_string(d).c_str() );
 }

为了避免警告,你可以将d转换为long long int。

printf("%lld", static_cast<long long int> (d));

Perhaps not directly related to the 32/64 bit problem at hand, but some of us are on embedded systems with odd output consoles and C++ libraries. (Plus we know that if we have to do any serious output formatting, printf is saner than iomanip!)

Anyway, this prints the guts of a duration and may be useful for debugging. Modify to taste.

template<typename Rep, typename Ratio>
printf_dur( std::chrono::duration< Rep, Ratio > dur )
{
    printf( "%lld ticks of %lld/%lld == %.3fs",
            (long long int) dur.count(),
            (long long int) Ratio::num,
            (long long int) Ratio::den,
            ( (Ratio::num == 1LL)
              ? (float) dur.count() / (float) Ratio::den
              : (float) dur.count() * (float) Ratio::num
            )
         );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM