作品:
std::chrono::duration<unsigned long long> test1 = std::chrono::seconds(1);
不起作用:
std::chrono::duration<unsigned long long> test2 = std::chrono::milliseconds(1);
为什么有区别?持续时间内部没有足够的粒度吗?
从毫秒值初始化持续时间的首选方法是什么?
答案 0 :(得分:4)
std::chrono::duration
的模板参数列表包含两个参数:用于保存基础数据的类型,和一个FROM node:9-alpine as builder
# Create a directory where our app will be placed. This might not be necessary
RUN mkdir -p /root
# Change directory so that our commands run inside this new directory
WORKDIR /root
# Get all the code needed to run the app
COPY . /root
WORKDIR /root
# Expose the port the app runs in
RUN npm install --global yarn@1.6.0
RUN chmod a+rwx /usr/local/lib/node_modules/yarn/bin/yarn*
RUN chmod a+rwx /usr/local/bin/yarn*
RUN yarn -v
RUN yarn install
RUN yarn add @material-ui/icons
EXPOSE 80
CMD yarn run web
From node:9-alpine
WORKDIR /root
COPY --from=builder /root .
RUN ls -la
EXPOSE 80
CMD yarn run web
参数,表示持续时间的指数。像std::ratio
和std::chrono::seconds
这样的类型是该模板的特化,分别使用std::chrono::milliseconds
和std::chrono::duration<int64_t, std::ratio<1>>
。
如果您没有为该类型提供std::chrono::duration<int64_t, std::ratio<1, 1000>>
参数,则默认为std::ratio
。
因此,您的自定义工期类型隐式采用std::ratio<1>
的形式,这使得它几乎等效于std::chrono::duration<unsigned long long, std::ratio<1>>
(唯一的区别是无符号值而不是一个带符号的值),但由于其比率高于提供给std::chrono::seconds
的比率,因此类模板禁止进行赋值/原始转换。在这种情况下,如果您希望分配完成,则需要显式转换它:
std::chrono::milliseconds
typedef std::duration<unsigned long long> my_duration;
//my_duration test1 = std::chrono::milliseconds(1);//Forbidden
my_duration test1 = std::chrono::duration_cast<my_duration>(std::chrono::milliseconds(1)); //Permitted, will be truncated
my_duration test2 = std::chrono::duration_cast<my_duration>(1ms); //Permitted, may be easier to read
参数表示持续时间的变动量。刻度大小越小,表示持续时间的基础数字越大。例如:
std::ratio