[ConceptGCC] |
ConceptGCC :Re: questions about concept_map<Stack<vector> > |
From: Martin Sebor (sebor_at_[hidden])
Date: 2007-03-04 20:49:55
TheChao wrote:
> You have declared all of your Stack functions as non-methods, i.e.
> rewrite foo like this:
Doh! I'd thought that might have been it and even changed the first
call as you suggest but got other (unrelated) errors that I hadn't
read carefully enough to get it.
>
[...]
> Note that Stack T makes no assumptions about integral types, so this
> is bad-code anyways: it assumes that value_type is convertible to
> Integral.
Thanks for the hint. I suppose it explains why I get an error for
the line: assert (1 == top (stk)); I.e., I didn't specify that foo
requires value_type to be equality-comparable and/or convertible
to int.
>
> You probably want this for your concept:
>
> concept Stack<typename T> {
> typename value_type = T::value_type;
> where Assignable<value_type> && CopyConstructible<value_type> &&
> DefaultConstructible<value_type>;
> // Adobe et al. call this 'Regular'
> void T::push ();
> value_type T::top () const;
> void T::pop ();
> bool T::empty () const;
> }
>
> Note that if your concept_map to this concept is anything other than
> nominative, i.e. if you have to 'add' a member function to meet the
> requirements, it will fail in ConceptGCC5. It may or may not work from
> the SVN.
Okay, thanks. I'll try to digest this :)
Martin
>
> -j.
>
> On 3/4/07, Martin Sebor <sebor_at_[hidden]> wrote:
>> I'm experimenting with the Stack<std::vector<T> > concept map
>> from N2081 (see my code below) and I'm getting errors I don't
>> understand. Can someone help me see what I'm doing wrong?
>>
>> The errors are:
>>
>> z.cpp: In function 'void foo()':
>> z.cpp:26: error: 'struct T' has no member named 'push'
>> z.cpp:27: error: 'struct T' has no member named 'empty'
>> z.cpp:28: error: 'struct T' has no member named 'top'
>> z.cpp:29: error: 'struct T' has no member named 'pop'
>> z.cpp:30: error: 'struct T' has no member named 'empty'
>>
>> Also, I'm wondering why it is desirable to require the author
>> of the concept map to define value_type and empty() when vector
>> provides identical members in its own interface.
>>
>> Thanks
>> Martin
>>
>> #include <vector>
>>
>> concept Stack<class T> {
>> typename value_type;
>> T::T ();
>> void push (T&, value_type);
>> void pop (T&);
>> value_type top (const T&);
>> bool empty (const T&);
>> };
>>
>> template <class T>
>> concept_map Stack<std::vector<T> > {
>> typedef T value_type;
>> void push (std::vector<T> &v, const value_type &x) { v.push_back (x); }
>> void pop (std::vector<T> &v) { v.pop_back (); }
>> value_type top (const std::vector<T> &v) { return v.back (); }
>> bool empty (const std::vector<T> &v) { return v.empty (); }
>> };
>>
>> template <Stack T>
>> void foo ()
>> {
>> T stk;
>> stk.push (1);
>> assert (!stk.empty ());
>> assert (1 == stk.top ());
>> stk.pop ();
>> assert (stk.empty ());
>> }
>>
>> int main ()
>> {
>> foo<std::vector<int> >();
>> }
>> _______________________________________________
>> ConceptGCC mailing list
>> ConceptGCC_at_[hidden]
>> http://www.osl.iu.edu/mailman/listinfo.cgi/conceptgcc
>>
> _______________________________________________
> ConceptGCC mailing list
> ConceptGCC_at_[hidden]
> http://www.osl.iu.edu/mailman/listinfo.cgi/conceptgcc