我想使用std::chrono::duration
文字,例如10s
表示“10 秒”,如下所示:
std::chrono::duration<uint64_t, std::milli> millisecs = 10s;
但是,我收到此错误:
main.cpp:20:17: error: unable to find numeric literal operator 'operator""s'
millisecs = 20s;
main.cpp:22:17: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
我已经添加-fext-numeric-literals
到我的 gcc 编译命令中:
g++ -fext-numeric-literals --std=c++17 -Wall -pedantic -Wextra \
main.cpp -O0 -g -o go
我究竟做错了什么?
You should add the corresponding namespace
:
using namespace std::chrono_literals;
Don't forget a using namespace
declaration to get access to std::chrono::duration<>
literals
I did some playing around and learning that you can take a look at here: chrono_duration_literals__using_declaration.cpp.
Key takeaways:
要使用“30 秒”std::chrono::duration<>
之类的文字30s
,例如:在此声明中:
std::chrono::seconds halfmin = 30s;
...您必须使用适当的using namespace
声明,例如:
// Use at least **one** of these!:
using namespace std::literals;
using namespace std::chrono_literals; // [my preference]
using namespace std::literals::chrono_literals;
using namespace std::chrono;
...或者您可以省略这些using namespace
声明,但是您必须将整数显式转换为一个std::chrono::duration<>
类型,例如 to ,std::chrono::seconds
它是 typedef std::chrono::duration<int64_t, ratio<1, 1>>
:
std::chrono::seconds halfmin = std::chrono::seconds(30);
我学习using namespace
上述 4 个选项的主要来源是:std::literals::chrono_literals::operator""s。这是文字运算符的页面s
,持续几秒钟。这个运算符称为operator""s()
。
无论如何,那个 cppreference.com 社区 wiki 提供了一些非常有价值的信息(强调):
笔记
These operators are declared in the namespace
std::literals::chrono_literals
, where bothliterals
andchrono_literals
areinline
namespaces. Access to these operators can be gained withusing namespace std::literals
,using namespace std::chrono_literals
, andusing namespace std::literals::chrono_literals
.In addition, within the namespace
std::chrono
, the directiveusing namespace literals::chrono_literals;
is provided by the standard library, so that if a programmer usesusing namespace std::chrono
; to gain access to the classes in thechrono
library, the corresponding literal operators become visible as well.
std::string
also definesoperator""s
, to represent literal objects of typestd::string
, but it is a string literal:10s
is ten seconds, but"10"s
is a two-character string.
If you leave out the using namespace
declaration, you have these crazy-long casting options
All of these options work too, withOUT including a using namespace
declaration from above, but they are pretty irritating to use:
std::chrono::seconds halfmin = (std::chrono::seconds)30; // WORKS!
std::chrono::seconds halfmin = std::chrono::seconds(30); // WORKS! [My preference]
std::chrono::seconds halfmin = std::chrono::seconds{30}; // WORKS!
std::chrono::seconds halfmin =
std::chrono::duration<int64_t, std::ratio<1>>(30); // WORKS!
std::chrono::seconds halfmin = std::chrono::duration<int64_t>(30); // WORKS!
auto halfmin = std::literals::chrono_literals::operator""s(30); // WORKS!
std::chrono::duration<long double, std::ratio<1>> halfmin =
std::literals::chrono_literals::operator""s(30); // WORKS!
std::chrono::seconds halfmin =
static_cast<std::chrono::seconds>(30); // WORKS!
std::chrono::seconds halfmin =
static_cast<std::chrono::duration<int64_t, std::ratio<1, 1>>>(30); // WORKS!
If you're looking for C-like simplicity instead of std::chrono
...then use my millis()
, micros()
, and nanos()
functions instead!
Then you can get super-simple timestamps like this:
// millisecond timestamp with monotonic clock in C or C++
uint64_t time_ms = millis();
// microsecond timestamp with monotonic clock in C or C++
uint64_t time_us = micros();
// nanosecond timestamp with monotonic clock in C or C++
uint64_t time_ns = nanos();
See my answers and libraries:
- Here is how to get simple C-like millisecond, microsecond, and nanosecond timestamps in C++:
- How to get a simple timestamp in C
- My Linux C and C++
timinglib.h
timestamp and timing library. It has some really convenientsleep
andsleep_until
functions as well, and allows you turn on the soft real-timeSCHED_RR
round-robin scheduler in Linux too, to improve sleep resolution in x86-64 Linux from ~55 us to ~4 us.- I keep this library up-to-date, and use it a lot. It's great.
- timinglib.h
- timinglib.c
- 10 KHz periodic loop example of mine on Linux using my
sleep_until_us()
function: How to run a high-resolution, high-precision periodic loop in Linux easily, at any frequency (ex: 10 KHz) using a soft real-time scheduler and nanosecond delays