简体   繁体   English

C ++-在模板类中显式调用构造函数模板?

[英]C++ - Invoking a constructor template explicitly inside a template class?

I have a class template called ScalarClamped. 我有一个名为ScalarClamped的类模板。 An instance of this class has a user-determined value range, and whenever a value assigned to it is out of it's bounds, the value will be clamped into the user-determined value range, hence the name "ScalarClamped": 此类的实例具有用户确定的值范围,并且每当为其分配的值超出其范围时,该值将被限制在用户确定的值范围内,因此名称为“ ScalarClamped”:

ScalarClamped<float> scalar__(75, 0, 100); // Parameters: current value (75), minimum allowed value (0), maximum allowed value(100).
scalar__ += 50;
std::cout << scalar__.value() << std::endl; // Output: 100.
scalar -= 150;
std::cout << scalar__.value() << std::endl; // Output: 0.

When implementing member function operator+ , I ran into a problem. 当实现成员函数operator+ ,我遇到了一个问题。 Here's an implementation of operator+ for one overload: 这是一个重载的operator+的实现:

template<typename T>
ScalarClamped<T> ScalarClamped<T>::operator+(ScalarClamped const& scalar_clamped_){
    return ScalarClamped<T>::ScalarClamped<T&&, T const&, T const&>(_m_tValue + scalar_clamped_._m_tValue, _m_tValueMin, _m_tValueMax);
}

As you can see, I'm trying to invoke a template constructor. 如您所见,我正在尝试调用模板构造函数。 Here's it's implementation (work in progress, may contain bugs as well): 这是它的实现(进行中的工作,可能还包含错误):

template<typename T>
template<typename TypeValue, typename TypeMin, typename TypeMax>
ScalarClamped<T>::ScalarClamped(TypeValue value_, TypeMin min_, TypeMax max_):
    // Initialization list:
    _m_tValue((std::is_lvalue_reference<TypeValue>::value) ? value_ : std::move(value_)),
    _m_tValueMax((std::is_lvalue_reference<TypeMax>::value) ? max_ : std::move(max_)),
    _m_tValueMin((std::is_lvalue_reference<TypeMin>::value) ? min_ : std::move(min_))
    // Function body:
    {
        Algorithm::clamp<T&, T const&, T const&>(_m_tValue, _m_tValueMin, _m_tValueMax);
    }

The main issue for me is that I'm unable to correctly call this constructor template. 对我而言,主要问题是我无法正确调用此构造函数模板。 I get the following error message ( shortened ): 我收到以下错误消息( 缩短了 ):

... error: dependent-name 'ScalarClamped<T>::ScalarClamped<T&&, const T&, const T&>' is parsed as a non-type, but instantiation yields a type|

Obviously I'm trying to invoke it the wrong way. 显然,我试图以错误的方式调用它。 How to invoke my constructor template appropriately? 如何适当调用我的构造函数模板?

It is impossible to provide explicit template arguments to constructor templates. 无法为构造函数模板提供显式模板参数。

The types must be inferred: 必须推断类型:

template<typename T>
template<typename TypeValue, typename TypeMin, typename TypeMax>
ScalarClamped<T>::ScalarClamped(TypeValue&& value_, TypeMin&& min_, TypeMax&& max_):
    // Initialization list:
    _m_tValue(std::forward<TypeValue>(value_)),
    _m_tValueMax(std::forward<TypeMax>(max_)),
    _m_tValueMin(std::forward<TypeMin>(min_))
    // Function body:
    {
        Algorithm::clamp<TypeValue, TypeMin, TypeMax>(_m_tValue, _m_tValueMin, _m_tValueMax);
    }

template<typename T>
ScalarClamped<T>
ScalarClamped<T>::operator+(ScalarClamped const& scalar_clamped_) const
{
    return ScalarClamped<T>::ScalarClamped(
        _m_tValue + scalar_clamped_._m_tValue,
        _m_tValueMin,
        _m_tValueMax);
}

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

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