【问题标题】:calling a function of template parameter in c++11 where template parameter is chrono::system_clock [closed]在 c++11 中调用模板参数的函数,其中模板参数是 chrono::system_clock [关闭]
【发布时间】:2013-09-13 15:46:54
【问题描述】:
     template< class CLOCK >
    std::string print_date_time( typename CLOCK::time_point p_time ){

        std::stringstream ss;

        std::time_t t = CLOCK::to_time_t(p_time);
        ss << std::ctime(&t) << std::endl;

        return ss.str();
    }

int main(){
std::cout << print_date_time( std::chrono::system_clock::now() );

}

我确实包含了正确的文件,让我知道哪里出错了。

【问题讨论】:

  • 您应该详细说明您的问题。你如何调用函数,你从编译器得到什么错误,...
  • 你怎么称呼它?它编译了吗?
  • 基于对您的问题实际是什么的快速猜测(基本上与 cpp 的代码相同),您可以通过拼写模板类型或将签名更改为 std::string print_date_time( typename std::chrono::time_point&lt;CLOCK&gt; p_time ) 来修复它。还没有弄清楚细节 - 模板参数推导让我感到困惑。还有,你为什么要刷stringstream
  • 或者,将TIME_POINT 设为模板参数,然后调用TIME_POINT::clock::to_time_t(p_time);

标签: c++ c++11 chrono


【解决方案1】:

我只是将我的答案作为一大段代码倾倒在这里,因为我担心我对模板参数推导没有很好的解释。

我认为这与std::chrono::system_clock::time_point 成为typedefstd::chrono::time_point&lt;std::chrono::system_clock&gt; 有关,但我不确定,我希望有人能给出一个很好的解释。在那之前,我只是倾倒了 2 个可行的解决方案。

#include <chrono>
#include <string>
#include <sstream>
#include <iostream>

template< class CLOCK >
std::string print_date_time( typename CLOCK::time_point p_time ){

    std::stringstream ss;

    std::time_t t = CLOCK::to_time_t(p_time);
    ss << std::ctime(&t) << std::endl;

    return ss.str();
}

// You can do this
template< class CLOCK >
std::string print_date_time_2( typename std::chrono::time_point<CLOCK> p_time ){

    std::stringstream ss;

    std::time_t t = CLOCK::to_time_t(p_time);
    ss << std::ctime(&t) << std::endl;

    return ss.str();
}


// Or this
template< class TIME_POINT >
std::string print_date_time_3( TIME_POINT p_time ){

    std::stringstream ss;

    std::time_t t = TIME_POINT::clock::to_time_t(p_time);
    ss << std::ctime(&t) << std::endl;

    return ss.str();
}


int main()
{
    std::cout
        << print_date_time
            <std::chrono::system_clock> // You don't want this!!
                                        // But without it you get template
                                        // argument deduction errors.
            (std::chrono::system_clock::now())
        << '\n';


    std::cout
        << print_date_time_2
            (std::chrono::system_clock::now())
        << '\n';

    std::cout
        << print_date_time_3
            (std::chrono::system_clock::now())
        << '\n';
}

【讨论】:

  • 是的,这正是我想做的。谢谢。
  • (简短版本的)解释是参数中:: 左侧的任何内容都是非推断上下文。想一想:编译器必须遍历所有可以想象的类型并检查它们中的每一个是否有一个与参数类型匹配的嵌套time_point类型。
【解决方案2】:

print_data_time() 函数没有问题。以下代码有效:

#include<sstream>
#include<iostream>
#include<chrono>
#include<ctime>

using namespace std;

template< class CLOCK >
std::string print_date_time( typename CLOCK::time_point p_time ){

    std::stringstream ss; 

    std::time_t t = CLOCK::to_time_t(p_time);
    ss << std::ctime(&t) << std::endl;

    return ss.str();
}

int main()
{
    chrono::system_clock::time_point now = chrono::system_clock::now();
    cout << print_date_time<chrono::system_clock>(now)  << endl; 

}

【讨论】:

  • 感谢这行得通,但我试图实现或想要做的是 print_date_time(now)。我的意思是为什么我必须提到模板参数 CLOCK 而不是由我正在使用的输入参数更新它。
  • 你为什么不一开始就问这个问题?
猜你喜欢
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 2012-02-21
  • 1970-01-01
  • 2012-08-14
  • 2023-03-18
相关资源
最近更新 更多