Vladimir Jovic <> writes:
> Paul Bibbings wrote:
>> Vladimir Jovic <> writes:
>>>> I can reproduce this error only with versions of GCC prior to the
>>>> 4.4 series:
>>>>
>>> Does it mean it was a bug in gcc, and is fixed now?
>>> Is the original example breaking rules of c++ standard?
>>
>> I would say that the former, certainly. I do not have the book by
>> Vandevoorde and Josuttis in which this example evidently appears, but I
>> would find it hard to believe that their code would be anything but
>> standard compliant. Having said that, I haven't checked it out in the
>> GCC bug lists.
>>
>
> Yes, I found it very strange that I can not compile the example in the
> OP.
I haven't got the book by Vandevoorde and Josuttis (though I really
should get myself a copy), but this thread did lead me to their example
code on the internet (
http://www.josuttis.de/tmplbook/) where I was
surprised to find that, in one place, there does appear to be a section
of code that, IMHO, fails to compile through being non-standard. (I
appreciate that the book, and code, was written in 2002, so it's
possible that there was a change between C++98 and C++03 that might have
broken the original code.)
In basics/stack7decl.hpp they have this class template
(
http://www.josuttis.de/tmplbook/basi...ecl.hpp.html):
template <typename T,
template <typename ELEM> class CONT = std::deque >
class Stack {
private:
CONT<T> elems; // elements
public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
};
This code is supplemented with appropriate definitions and used with
implicit instantiations through, for example:
Stack<int> intStack; // (in basics/stack7test.cpp)
This is the kind of thing that I remember compiled without problem using
some thing like gcc-4.3.4 or earlier. However, it would not compile
with more recent versions, and I believe these newer versions are
correct.
The problem is that the type std::deque has *two* template parameters,
the second being the allocator which defaults to std::allocator<T> (on
std::deque<T>). I believe (though, of course, I'm open to being
corrected) that this second template parameter cannot be ignored in the
above code, even though a default is provided. I think this needs to be
something along the lines of:
template< typename T,
typename Alloc = std::allocator< T >,
template< typename ELEM, typename Alloc> class CONT =
std::deque >
class Stack {
private:
CONT< T, Alloc > elems;
// ... etc.
};
Certainly gcc-4.4.3 requires this form of adjustment in order to enable
instantiation using Stack<int>, etc.
Regards
Paul Bibbings