0

I have a templated function which generates a list of a certain type. For each type the list is formed by an overloaded function. Roughly it looks something like this

void Process(std::vector<Type1> &vector){
}

void Process(std::vector<Type2> &vector){
}

void Process(std::vector<Type3> &vector){
}

template <class T>
std::vector<T> DoFoo() {
    std::vector<T> my_vector;
    ...
    Process(my_vector);
    ...
    return my_vector;
}

However now the Process function for Type3 requires a second argument, the rest stays the same. For now I have just added this argument into all 3 of the Process functions but it's ugly and inelegant. Is there a way to implement it better?

1 Answer 1

2

An option would be something like this:

void Process(std::vector<Type2> &vector);
void Process(std::vector<Type3> &vector, const Config& config);

template <class T, typename...ProcessParams>
std::vector<T> DoFoo(ProcessParams&&... process_params) {
    std::vector<T> my_vector;
    ...
    Process(my_vector, std::forward<ProcessParams>(process_params)...);
    ...
    return my_vector;
}

You'd still be able to call DoFoo with Type2:

std::vector<Type2> result = DoFoo<Type2>();

but then would need to call it with an extra argument for Type3:

std::vector<Type3> result = DoFoo<Type3>(Config{});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.