'Can I use template typedef without clarification
I have BadArray class in Array class for exeptions.
template <size_t n, typename T>
class Array<n, T>::BadArray {
private:
string _reason;
size_t _index;
public:
BadArray(const string& reason = "", const size_t index = 0);
~BadArray();
const string reason() const;
const size_t index() const;
};
And this operator<<
template <size_t n, typename T>
ostream& operator<<(ostream& os, const typename Array<n, T>::BadArray& seq) {
os << seq.reason() << " " << seq.index();
return os;
}
So does exist some way to use BadArray in try catch without clarification of size_t and typename ?
int main()
{
try {
Array<3, double> array2{};
array2[3] = 4;
}
catch (Array<size_t n, typename T>::BadArray ba) {
cout << ba;
}
}
Solution 1:[1]
A simple solution is to define the class outside of the template:
class BadArray {
// ...
};
// ...
catch (BadArray& ba)
P.S. As shown in the example, I recommend catching a reference to avoid unnecessary copying.
P.P.S I recommend following the common convention of inheriting the std::exception hierarchy. With the inheritance and the virtual functions that come with it, it will also become important to catch reference so that you don't accidentally slice the base off a derived exception.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
