2

std::put_time does not seem to work.

The following snippet is taken directly from en.cpp.reference.com.

#include <iostream>
#include <iomanip>
#include <ctime>

int main()
{
    std::time_t t = std::time(nullptr);
    std::tm tm = *std::localtime(&t);
    std::cout.imbue(std::locale("ru_RU.utf8"));
    std::cout << "ru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
    std::cout.imbue(std::locale("ja_JP.utf8"));
    std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
}

Several compilers agree that put_time is not a member of the std library. My question is; why? Does put_time work, and if so, with which compilers and options does the example compile successfully?

clang++ 3.5.0-10 (based on LLVM 3.5.0) with -std=c++14 complains thus:

main.cc:10:36: error: no member named 'put_time' in namespace 'std'
    std::cout << "ru_RU: " << std::put_time(&tm, "%c %Z") << '\n';
                              ~~~~~^
main.cc:12:36: error: no member named 'put_time' in namespace 'std'
    std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
                              ~~~~~^
2 errors generated.

Gcc added support for std::put_time in version 5.0, as stated here by Jonathan Wakely.

A third "compiler" (using cpp.sh) and ticking c++14, gives:

 In function 'int main()':
10:31: error: 'put_time' is not a member of 'std'
12:31: error: 'put_time' is not a member of 'std'

I'm trying to compile this on a raspberry pi; full compiler version info:

pi@tacarmepi:~/test$ clang++ --version
Raspbian clang version 3.5.0-10+rpi1 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: arm-unknown-linux-gnueabihf
Thread model: posix
TamaMcGlinn
  • 2,840
  • 23
  • 34
  • 2
    Possible duplicate of [std::put\_time implementation status in GCC?](https://stackoverflow.com/questions/14136833/stdput-time-implementation-status-in-gcc) – Rakete1111 Jul 05 '17 at 15:43
  • Thank you Rakete; that explains why my g++ version 4.9.2 was not sufficient. The question may still be answered completely by specifying which compilers actually support the feature. In particular, I want to use clang, whose llvm page claims that the feature should be implemented as of Clang 3.3. – TamaMcGlinn Jul 05 '17 at 15:59
  • I guess, but I don't think that is on-topic. Anyways, are you sure that you are using LLVM's Standard Library instead of gcc's? Because you need a compiler flag for that in clang last time I checked. – Rakete1111 Jul 05 '17 at 16:00
  • Unsure; so you mean I should not try `clang++ -std=c++1z main.cc` but `clang++ -std=c++1z -stdlib=libstdc++ main.cc` or `clang++ -std=c++1z -stdlib=libc++ main.cc`? I've tried both; libc++ fails to find iostream, libstdc++ fails the same way as without passing any library option. – TamaMcGlinn Jul 05 '17 at 16:15
  • `-stdlib=libc++` is indeed the right option, not sure why it can't find `` though... – Rakete1111 Jul 05 '17 at 16:17

0 Answers0