What is s in std::this_thread::sleep_for(2s)?

Zhang

I find the std::this_thread::sleep_for can process the time unit s.

std::this_thread::sleep_for(2s);

But I don't know what the s in 2s is.

YSC

What is s in std::this_thread::sleep_for(2s)?

s is a user-defined literal making 2s a literal value of type chrono::second.


Built-in literals

You might be familiar with integer literals and floating literals; those are built-in suffixes:

+--------+---------+---------------+
| Suffix | Example |     Type      |
+--------+---------+---------------+
|      U |     42U | unsigned int  |
|     LL |     1LL | long long int |
|      f |   3.14f | float         |
+--------+---------+---------------+

They let you provide a literal value whose type matches your needs. For example:

int   half(int n)   { return n/2; }
float half(float f) { return f/2; }
half(3);   // calls the int   version, returns 1    (int)
half(3.f); // calls the float version, returns 1.5f (float)

User-defined literals

C++11 added a new feature: user-defined literal suffixes:

Allows integer, floating-point, character, and string literals to produce objects of user-defined type by defining a user-defined suffix.

Syntax

They allow to provide a literal of a user-defined type or a Standard Library-defined type. Defining a literal is as easy as defining the operator"":

// 0_<suffix> is now a <type> literal
<type> operator "" _<suffix>(unsigned long long); // ull: one of the height existing forms

Example

#include <iostream>

class Mass
{
    double _value_in_kg;
public:
    Mass(long double kg) : _value_in_kg(kg) {}
    friend Mass          operator+ (Mass const& m1, Mass const& m2)  { return m1._value_in_kg + m2._value_in_kg; }
    friend std::ostream& operator<<(std::ostream& os, Mass const& m) { return os << m._value_in_kg << " kg"; }
};

Mass operator "" _kg(long double kg) { return Mass{kg}; }
Mass operator "" _lb(long double lb) { return Mass{lb/2.20462}; }

int main()
{
    std::cout << 3.0_kg + 8.0_lb  << '\n';
}

Outputs "6.62874 kg" (demo) as it should.

The case of std::chrono

Unlike "real" user-provided literals, the Standard Library provides literals not starting with an underscore (_). s is one of them and is defined in <chrono> (since C++14):

constexpr chrono::seconds operator "" s(unsigned long long secs);

With other duration literals, it let you write something as pretty as:

#include <chrono>
using namespace std::chrono_literals;
const auto world_marathon_record_2018 = 2h + 1min + 39s;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is `std::this_thread::yield()` 10x slower than `std::this_thread::sleep_for(0s)`?

std::this_thread::sleep_for() freezing the program

std::condition_variable not working with std::this_thread::sleep_for()

std::this_thread::sleep_for() vs sleep() in main()

Code generated - usleep vs std::this_thread::sleep_for

Can std::this_thread::sleep_for() have spurious wakeups?

std::this_thread::sleep_for sleeps for too long

std::this_thread::sleep_until timing is completely off by about a factor of 2, inexplicably

Where is std::this_thread for jthread?

How to make a thread stop excution (eg: std::this_thread::sleep_for) for an accturate interval

std::this_thread::sleep_for sleeps shorter than expected in VS2015

Why doesn't std::this_thread::sleep_for() work in MacOS terminal?

C++11 std::this_thread - How to cancel sleep_until ()?

C++11 std::this_thread::sleep_until() hangs when compiled with GCC 4.8.5

Does std::condition_variable::wait_until have any advantage against std::this_thread::sleep_for?

std::thread class vs std::this_thread namespace in c++?

std::exception's what() returns "std::exception"

What's the equivalent of Java's Thread.sleep() in JavaScript?

What's wrong with std::valarray's operator*?

this_thread::sleep_for sleeps early

Can this_thread::sleep() be interrupted on linux?

Does std::thread make any guarantee about what's happened when its constructor returns?

Is it okay to use std::this_thread* functions from boost::threads?

what's wrong with std::remove_if in this code

What's wrong with this inline initialization of std::array?

What's the point of std::remove_reference

What's the purpose of std::to_integer?

What's the difference between std::endl and '\n'

What's the default value for a std::atomic?