首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cpp中的main()中正确调用此模板函数,cpp中使用时间库将数字转换为日期?

如何在cpp中的main()中正确调用此模板函数,cpp中使用时间库将数字转换为日期?
EN

Stack Overflow用户
提问于 2022-02-25 15:30:24
回答 1查看 50关注 0票数 0

如何在cpp中的main()中正确调用此模板函数,cpp中使用时间库将数字转换为日期?

代码语言:javascript
复制
 #include <iostream>
 #include <chrono>
 #include <tuple>
 
 //using namespace std;
 // Returns year/month/day triple in civil calendar
 // Preconditions:  z is number of days since 1970-01-01 and is in the range:
 //                   [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
 template <class Int>
 constexpr
 std::tuple<Int, unsigned, unsigned>
 civil_from_days(Int z) noexcept
 {
     static_assert(std::numeric_limits<unsigned>::digits >= 18,
              "This algorithm has not been ported to a 16 bit unsigned integer");
     static_assert(std::numeric_limits<Int>::digits >= 20,
              "This algorithm has not been ported to a 16 bit signed integer");
     z += 719468;
     const Int era = (z >= 0 ? z : z - 146096) / 146097;
     const unsigned doe = static_cast<unsigned>(z - era * 146097);          // [0, 146096]
     const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365;  // [0, 399]
     const Int y = static_cast<Int>(yoe) + era * 400;
     const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100);                // [0, 365]
     const unsigned mp = (5*doy + 2)/153;                                   // [0, 11]
     const unsigned d = doy - (153*mp+2)/5 + 1;                             // [1, 31]
     const unsigned m = mp < 10 ? mp+3 : mp-9;                            // [1, 12]
     return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
 }
 
 
 int main(){
    std::cout<< civil_from_days(15432)<<'\n';
 }

这会导致这个(相同)编译器错误。

代码来自霍华德·辛南特

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-25 15:37:21

对函数的调用不是问题所在。您得到的错误(应该包含在问题中)是因为元组没有预定义的输出运算符。但是,您可以打印单个成员:

代码语言:javascript
复制
int main(){
    auto res = civil_from_days(15432);
    std::cout<< std::get<0>(res)<<'\n';
    std::cout<< std::get<1>(res)<<'\n';
    std::cout<< std::get<2>(res)<<'\n';
 }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71268121

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档