我想使用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 both literals and chrono_literals are inline namespaces. Access to these operators can be gained with using namespace std::literals, using namespace std::chrono_literals, and using namespace std::literals::chrono_literals.

In addition, within the namespace std::chrono, the directive using namespace literals::chrono_literals; is provided by the standard library, so that if a programmer uses using namespace std::chrono; to gain access to the classes in the chrono library, the corresponding literal operators become visible as well.

std::string also defines operator""s, to represent literal objects of type std::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:

  1. Here is how to get simple C-like millisecond, microsecond, and nanosecond timestamps in C++:
  2. How to get a simple timestamp in C
  3. My Linux C and C++ timinglib.h timestamp and timing library. It has some really convenientsleep and sleep_until functions as well, and allows you turn on the soft real-time SCHED_RR round-robin scheduler in Linux too, to improve sleep resolution in x86-64 Linux from ~55 us to ~4 us.
    1. I keep this library up-to-date, and use it a lot. It's great.
    2. timinglib.h
    3. timinglib.c
  4. 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

你有适当的“使用命名空间”指令吗?

@Mat:运算符使用哪个名称空间?

en.cppreference.com/w/cpp/chrono/operator%22%22s

@Mat:好的,明白了。谢谢!让它回答接受它;)

'chrono_literals' is not a namespace-name

What do you mean? What is that then?!

Exception, that was thrown after a namespace addition. From other threads I found it not compatible with older C++ versions (<=11)