Hello, I have a problem with writing a catch clause for an exception that is a class nested in a template. To be more specific, I have a following definition of the template and exception:
/** Generic stack implementation.
Accepts std::list, std::deque and std::vector
as inner container. */
template <
typename T,
template <
typename Element,
typename = std::allocator<Element>
> class Container = std::deque
>
class stack {
public:
class StackEmptyException { };
...
/** Returns value from the top of the stack.
Throws StackEmptyException when the stack is empty. */
T top() const;
...
}
I have a following template method that I want exception to catch:
template <typename Stack>
void testTopThrowsStackEmptyExceptionOnEmptyStack() {
Stack stack;
std::cout << "Testing top throws StackEmptyException on empty stack...";
try {
stack.top();
} catch (Stack::StackEmptyException) {
// as expected.
}
std::cout << "success." << std::endl;
}
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 any help!
What is interesting, if the stack implementation was not a template, then the compiler would accept the code as it is.
PS. I also tried redefining the template method type, but I couldn't make this work.