简体   繁体   English

Template Class,C ++的默认值

[英]Default value for Template Class, C++

Is there a way to give a default value for a template class? 有没有办法为模板类提供默认值?

Eg Say I have a class TimeSeries , that is templated. 例如,假设我有一个TimeSeries类,这是模板化的。 And I have a function called Foo() that returns T . 我有一个名为Foo()的函数返回T

template <typename T>
class TimeSeries
{
    T foo();
}

I want Foo() to do some calculation and return something of type T . 我希望Foo()做一些计算并返回T类型的东西。 But in case it can't, I want it to give a default value. 但如果它不能,我希望它给出一个默认值。 If T was double , I want that to be NaN . 如果Tdouble ,我希望那是NaN If T was int , I want that to be 0 . 如果Tint ,我希望它为0

TimeSeries<double> t;
t.foo(); // returns NAN

TimeSeries<int> t;
t.foo(); // returns 0

How would I achieve that? 我怎么做到这一点?

A bad solution is for Foo() to take in a default value. 一个糟糕的解决方案是Foo()接受默认值。

template <typename T>
class TimeSeries
{
    T foo(T defaultValue)
    {
       T ret;
       // if OK
       ret = computeValue();
       // if not OK
       ret = defaultValue;
       return ret;
    }
}

So default value would be 0 , or NaN , depending on the user. 因此默认值为0NaN ,具体取决于用户。

Or am I asking for a flawed design in the first place? 或者我首先要求一个有缺陷的设计?

EDIT - A logical question would be to ask what happens if T is not int or double . 编辑 - 一个逻辑问题是询问如果T不是intdouble会发生什么。 What does a default value even mean? 默认值甚至意味着什么?

The way I'm using TimeSeries , T is not exactly a general purpose type. 我使用TimeSeries的方式, T并不完全是通用类型。 It could only be 1 of int , double , and string . 它只能是intdoublestring I thought about simply writing 3 separate TimeSeries classes, but that seems like a flawed approach. 我想过简单地编写3个单独的TimeSeries类,但这似乎是一个有缺陷的方法。

I'm open to other ideas. 我对其他想法持开放态度。

You could declare the following template, default_value<> : 您可以声明以下模板default_value<>

template<typename>
struct default_value;

Then, provide full specializations for int and double with a member called value which provides the desired default value: 然后,使用名为value的成员为intdouble提供完全特化,该成员提供所需的默认值:

template<>
struct default_value<int> {
   static constexpr int value = 0;
};

template<>
struct default_value<double> {
   static constexpr double value = NaN;
};

Then, in your foo function template, you can simply return that value: 然后,在您的foo函数模板中,您只需返回该值:

return default_value<T>::value;

which will depend on T . 这将取决于T

since C++14 you can use a variable template : 从C ++ 14开始,您可以使用变量模板

template <class T> constexpr T default_value = {};
template <>
constexpr double default_value<double> = std::numeric_limits<double>::quiet_NaN();
template <typename T> class TimeSeries
{
    auto foo() -> T
    {
       T ret;

       if (condition)
         return ret;
       else
         return default_value<T>;
    }
};

Or am I asking for a flawed design in the first place? 或者我首先要求一个有缺陷的设计?

As far as I'm concerned it's a valid requirement. 就我而言,这是一个有效的要求。

I see two ways you can address that requirment. 我认为有两种方法可以满足这一要求。

  1. You can use the default_value approach as suggested in the other answer . 您可以使用另一个答案中建议的default_value方法。

  2. If you have the option of using C++17, you can use std::optional . 如果您可以选择使用C ++ 17,则可以使用std::optional

    If you don't have the option of using C++17, you can emulate std::optional in a less sophisticated way by changing the return value to std::pair<boo, T> with the understanding that the first of the returned value is set to false if the computation was not successful and to true if the computation was successful. 如果您没有使用C ++ 17的选项,您可以通过将返回值更改为std::pair<boo, T> ,以不太复杂的方式模拟std::optional ,同时理解第first如果计算不成功,则返回值设置为false如果计算成功,则返回true

Personally, I would prefer the second solution. 就个人而言,我更喜欢第二种解决方案。 It does not rely on sentry values. 它不依赖于哨兵值。 It captures the state of whether the computation was successful in a clear and explicit manner. 它以清晰明确的方式捕获计算是否成功的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM