如何使用std :: chrono :: duration作为模板参数? [英] How can I use std::chrono::duration as a template parameter?

查看:679
本文介绍了如何使用std :: chrono :: duration作为模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类,例如:

  template< typename T,size_t Seconds> class MyClass {} 



现在,我想将Seconds改为一个duration,用 std :: chrono :: duration 参数化。例如,我想能够这样做:

  MyClass< std :: string,std :: chrono :: seconds(30)>目的; 

此外,在模板中,我想指定一个默认值,例如 std :: chrono :: seconds(30)

解决方案

模板以一种聪明的方式:

 模板< typename T,typename Duration = std :: chrono :: seconds,int duration_value = 30> 
class MyClass
{
//现在你可以在这里使用持续时间:
// auto duration = Duration(duration_value);
}

然后你可以将你的模板实例化为

  MyClass< std :: string,std :: chrono :: seconds,30>目的; 

或者,实际上这些值为默认值,只需

  MyClass< std :: string>目的; 



编辑



考虑到PaperBirdMaster 的要求,您可以限制模板的 Duration 类型, std :: chrono :: duration ,这样:

  template< typename T> 
struct is_chrono_duration
{
static constexpr bool value = false;
}

template< typename Rep,typename Period>
struct is_chrono_duration< std :: chrono :: duration< Rep,Period>>
{
static constexpr bool value = true;
}

template< typename T,typename Duration = std :: chrono :: seconds,int duration_value = 30>
class MyClass
{
static_assert(is_chrono_duration< Duration> :: value,duration必须为std :: chrono :: duration);
//现在你可以在这里使用持续时间:
// auto duration = Duration(duration_value);
}

int main(int argc,char ** argv)
{
MyClass< std :: string,std :: chrono :: seconds,1> obj1; // Ok
MyClass< std :: string,std :: chrono :: milliseconds,1> obj2; // Ok
MyClass< std :: string,int,1> obj3; //错误
return 0;
}


I have a template class, something like:

template < typename T, size_t Seconds > class MyClass {}

Now, I would like to change Seconds to be a duration, so the class can be parametrized with std::chrono::duration. For example, I'd like to be able to do this:

MyClass < std::string, std::chrono::seconds(30) > object;

Also, in the template, I'd like to specify a default value, something like std::chrono::seconds(30).

解决方案

You can design your template in a clever way:

template < typename T, typename Duration = std::chrono::seconds, int duration_value = 30 > 
class MyClass 
{
    // Now you can use duration here:
    // auto duration = Duration(duration_value);
};

And then you can instantiate your template as

MyClass < std::string, std::chrono::seconds, 30 > object;

Or, actually having these values as defaults, simply

MyClass < std::string > object;

Edit:

Taking into account PaperBirdMaster's request, you can limit template's Duration type, to be std::chrono::duration only, this way:

template <typename T>
struct is_chrono_duration
{
    static constexpr bool value = false;
};

template <typename Rep, typename Period>
struct is_chrono_duration<std::chrono::duration<Rep, Period>>
{
    static constexpr bool value = true;
};

template < typename T, typename Duration = std::chrono::seconds, int duration_value = 30 >
class MyClass
{
    static_assert(is_chrono_duration<Duration>::value, "duration must be a std::chrono::duration");
    // Now you can use duration here:
    // auto duration = Duration(duration_value);
};

int main(int argc, char ** argv)
{
    MyClass < std::string, std::chrono::seconds, 1> obj1;       // Ok
    MyClass < std::string, std::chrono::milliseconds, 1> obj2;  // Ok
    MyClass < std::string, int, 1> obj3;                        // Error
    return 0;
}

这篇关于如何使用std :: chrono :: duration作为模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆