I have a problem writing a catch clause for an exception, which is a class nested in a template. To be more specific, I have the following template definition and exceptions:
template < typename T, template < typename Element, typename = std::allocator<Element> > class Container = std::deque > class stack { public: class StackEmptyException { }; ... T top() const; ... }
I have the following template method that I want to exclude for catch:
template <typename Stack> void testTopThrowsStackEmptyExceptionOnEmptyStack() { Stack stack; std::cout << "Testing top throws StackEmptyException on empty stack..."; try { stack.top(); } catch (Stack::StackEmptyException) {
When I compile it (-Wall, -pedantic), I get the following error:
In function 'void testTopThrowsStackEmptyExceptionOnEmptyStack()': error: expected type-specifier error: expected unqualified-id before ')' token === Build finished: 2 errors, 0 warnings ===
Thanks in advance for your help!
Interestingly, if the stack implementation was not a template, then the compiler will accept the code as is.
PS. I also tried to override the type of the template method, but I could not do the job.
Karol source share