C++11 chrono in visual studio 2013

c++, c++-chrono, c++11, visual-studio-2013

Solution

Below I post a SSCCE that focuses on your problem:

#include <chrono>

using namespace std::chrono;

int main() {
  auto now = system_clock::now();
  time_point<system_clock> epoch;
  microseconds ms = duration_cast<milliseconds>(now - epoch);
  microseconds hs = std::chrono::hours(1);
  auto mm = ms % hs;
}

Although the above example works on GCCv4.9 and CLANGv3.4. It fails to compile in VS2013.

The error reports that VC++ can't convert `std::chrono::microseconds` to `std::chrono::system_clock::rep`.

It seems that the implementers are messing something with the conversions, I consider this is a visual C++ bug that should be reported.

Problem

I am porting a code from Linux to Window. And There is a error i didn't expect in using std::chrono. since std::chrono is C++ standard library, i expected that it is working without modification. below is the code showing the error. the error happen at the parts where i use operator with duration instances and duration_cast function `with no instance of function template`. in Linux, the code works normally ``` std::string ChronoTimer::currentTime(){ using namespace std::chrono; auto now = system_clock::now(); time_point<system_clock> epoch; microseconds ms = duration_cast<milliseconds>(now - epoch); hours hour = duration_cast<hours>((ms % hours(24)) + hours(9)); minutes min = duration_cast<minutes>(ms % hours(1)); seconds sec = duration_cast<seconds>(ms % minutes(1)); milliseconds msec = duration_cast<milliseconds>(ms % seconds(1)); std::stringstream strStream; strStream << std::setfill('0') << std::setw(2) << hour.count() << ":"; strStream << std::setfill('0') << std::setw(2) << min.count() << ":"; strStream << std::setfill('0') << std::setw(2) << sec.count() << "."; strStream << std::setfill('0') << std::setw(3)<< msec.count(); return strStream.str(); } ``` ``` 1 IntelliSense: no instance of function template "std::chrono::duration_cast" matches the argument list argument types are: (<error-type>) 2 IntelliSense: no operator "+" matches these operands operand types are: std::chrono::system_clock::rep + std::chrono::hours 3 IntelliSense: no instance of function template "std::chrono::duration_cast" matches the argument list argument types are: (std::chrono::system_clock::rep) ```

Original source