Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:29:54+00:00 2026-06-11T01:29:54+00:00

I stumbled upon a strange error C2440: ” : cannot convert from ‘_CR’ to

  • 0

I stumbled upon a strange error

C2440: ” : cannot convert from ‘_CR’ to ‘std::chrono::milliseconds’

in what amounts basically to Howard Hinnant’s code in another question.

Should this compile on Visual Studio 2012 RC? What would be the reason for this problem? How about a fix or a workaround? My aim is just to create a simple timer (nothing too serious), so if there exists something to that effect, point will be taken — as well as other implementation cues.

The code in question is as follows.
Usage:

  timers::stopwatch w;
  w.start();
  std::cout << to_milliseconds(w.elapsed()) << std::endl;

And the header file is (implementation ommitted for breverity)

namespace timers
{
    class stopwatch
    {
        public:
            typedef std::chrono::high_resolution_clock clock;
            typedef clock::time_point time_point;
            typedef clock::period period;
            typedef std::chrono::duration<float, period> duration;

            stopwatch();
            ~stopwatch();

            void start();
            void stop();
            void restart();
            void reset();

            duration elapsed() const;
       private:
            clock timer_;
            time_point start_point_;
            time_point end_point_;
            bool is_running_;

            void start_measuring();
            void stop_measuring();
     };
}

//Some convenience stuff...
namespace
{       
    long long to_milliseconds(timers::stopwatch::duration const& duration) { return   std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); }
    long long to_nanoseconds(timers::stopwatch::duration const& duration) { return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count(); }
    long long to_seconds(timers::stopwatch::duration const& duration) { return std::chrono::duration_cast<std::chrono::seconds>(duration).count(); }

}

Edit

As as per Howard Hinnant’s note, there’s #include <chrono> in the header, the implementation file and the calling file. The error points to MS <chrono> header and the error is triggered in this code

// duration_cast
template<class _To,
    class _Rep,
    class _Period> inline
    typename enable_if<_Is_duration<_To>::value,
        _To>::type
        duration_cast(const duration<_Rep, _Period>& _Dur)
        {    // convert duration to another duration
            typedef typename ratio_divide<_Period, typename _To::period>::type _CF;
            typedef typename common_type<
            typename common_type<typename _To::rep, _Rep>::type,
                intmax_t>::type _CR;
                if (_CF::num == 1 && _CF::den == 1)
                    return (_To(static_cast<typename _To::rep>(_Dur.count())));
                else if (_CF::num != 1 && _CF::den == 1)
                    return (_To(static_cast<typename _To::rep>(static_cast<_CR>(_Dur.count())) * static_cast<_CR>(_CF::num)));
                else if (_CF::num == 1 && _CF::den != 1)
            return (_To(static_cast<typename _To::rep>(static_cast<_CR>(_Dur.count()) / static_cast<_CR>(_CF::den))));
                else
                    return (_To(static_cast<typename _To::rep>(xtatic_cast<_CR> _Dur.count()) * static_cast<_CR>(_CF::num) / static_cast<_CR>(_CF::den))));
}

and specifically on line 573 of <chrono>, which on the aforementioned is

 [...]
 else if (_CF::num != 1 && _CF::den == 1)
     return (_To(static_cast<typename _To::rep>(static_cast<_CR>(_Dur.count())) * static_cast<_CR>(_CF::num)));

I can’t really follow the purpose of all of that code (yet, at least) to have a definite opinition if it’s something in my code somewhere or in the VC++ <chrono> header. I recognise some of naming from TC1/SC22/WG21 n2661. In any event, I’ll get back to computer later and see if isolating the stopwatchto a project of its own affects the occurence of this error.

Edit 2

I put the code to an empty project and the problem is still there. As an added note, for the three to_*seconds calls there are six compiler errors. If I put the convenience methods to comments, the code compiles and runs.

Hmm, I wonder what would be a good work-around (or fix)..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:29:56+00:00Added an answer on June 11, 2026 at 1:29 am

    I think that the compile-time error is due to this bug in Visual Studio 2012 RC :

    https://connect.microsoft.com/VisualStudio/feedback/details/752794/std-chrono-duration-cast-lacks-double-support

    To workaround it, use a duration based on integral type instead of floating point :

    #include <cstdint>
    //...
    typedef std::chrono::duration<std::uint64_t, period> duration;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I stumbled upon a very strange bit of PHP code. Could someone explain why
I've stumbled upon a rather strange issue today with Spring 3.0: There's an abstract
Our project does some Java bytecode instrumentation. And we stumbled upon some strange behavior.
I stumbled upon a problem from the last Facebook Hacker Cup (so it's NOT
I stumbled upon this question from two years ago. Is there a way to
Making a footer for a web site I stumbled upon some strange behavior of
I stumbled upon this code: static void Main() { typeof(string).GetField(Empty).SetValue(null, evil);//from DailyWTF Console.WriteLine(String.Empty);//check //how
I am developing a Rails 2.3.8 app and I stumbled upon a strange form
I've stumbled upon a strange problem. I made a regular os x system update
I've stumbled upon a strange thing in Flex. I created an integer variable: var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.