Question

I have a class template called 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<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. Here's an implementation of operator+ for one overload:

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?

Was it helpful?

Solution

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top