Search code examples
c++functiontemplatesparametersparameter-passing

Function declaration returns 'error: parameter 'array' may not appear in this context'


I have a function that I'm trying to have use one of its parameters for another one of its parameters. Declaration:

template<num_type T> void sort(vector<T> &array, bool ltog = true, VecRange range = VecRange(0, array.size() - 1));

When I try to do this, the compiler throws an error: error: parameter 'array' may not appear in this context. My question is can I do this? If so, how?


Solution

  • You may not refer to parameters in parameter declarations if they are evaluated. You can refer them only in an unevaluated context as for example used in the sizeof operator.

    In your function declaration

    template<num_type T> void sort(vector<T> &array, bool ltog = true, VecRange range = VecRange(0, array.size() - 1));
    

    the parameter array is used in the evaluated expression VecRange(0, array.size() - 1).

    An example when such a reference tp a parameter in a parameter declaration list is valid

    template <typename T>
    void f( const T &x, size_t y = sizeof( x ) );