Reputation: 878
I found this StackOverflow question while looking for some sort of way to abstract data types.
I wanted to create an IO helper function that takes as parameters a class(more often than not a string) and a data type.
I'm also doubting the variable y
part. I don't know if the syntax is correct if I wanted y's value to change.
template <class message>
template <typename variable>
void inputCheck(message x, variable y)
{
cout << x;
cin >> y;
// if input y is invalid, call inputCheck again
// else, keep the input and assign it to y located outside this function
}
Upvotes: 0
Views: 80
Reputation: 75825
template <class OutputType, class InputType>
void InputCheck(const OutputType &x, InputType &y) {
cout << x;
cin >> y;
}
Also pay attention at InputType &y
: y
needs to be passed as a referenced so that its modification can be seen outside the function.
x
is passed as const &
because if OutputType
is large (a struct, a string or a vector etc.) passing by reference is way faster. The const
assures that it won't be modified.
Upvotes: 1