This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Issue with pointer redirection in inherited clauses


Hi Tyler --

Try putting your using clauses inside the class instead of inside the function.

template <typename T>
class SelfOrganizingList: public list<T>
{
    typedef list<T> super;

    using super::begin;
    using super::end;
    ...


  Brian

On Tue, May 5, 2009 at 7:30 PM, Tyler Earman <rem.intellegare@gmail.com> wrote:
> Hmm that's good advice. ?I can't seem to get it to compile though.
> Here's the original set of code
> template <typename T>
> class SelfOrganizingList: public list<T>
> {
> public:
> ?bool contains(const T& t)
> ?{
> ? ?typename list<T>::iterator it = find(this->begin(), this->end(), t);
> ? ?if (it == this->end()) return false;
> ? ?if (it != this->begin())
> ? ?{ erase(it);
> ? ? ?push_front(t);
> ? ?}
> ? ?return true;
> ?}
> };
>
> Using your advice, I changed the typename list code(line 7) to
>
> ? ?typedef list<T> super;
> ? ?using super::size;
> ? ?using super::empty;
> ? ?typedef typename super::iterator it;
>
> However it says that super is not a namespace (strlist.cpp:14: error:
> ‘SelfOrganizingList<T>::contains(const T&)::super’ is not a namespace)
>
> Any advice regarding this issue?
>
> I'm basically looking for a more elegant way of implementing this other
> than using this->begin(); because that seems a bit contradictory to the
> actual reason for the existence of the -> object.
>
> Thanks guys,
> Tyler
>
> John Fine wrote:
>> Tyler Earman wrote:
>>> "I have noticed that when (this is C++) inheriting a templatized derived
>>> class from a templatized base that in g++, references to elements of the
>>> base class have to be qualified with this-> or with the name of the base
>>> class put in front. ?But this is NOT needed in the Windows C++ compilers
>>> so far as I can see.
>> GCC is doing the correct thing according to the C++ standard. ?I
>> personally think the C++ standard is bad and this feature makes C++ a
>> worse language than it was before compilers enforced this language
>> behavior.
>>
>> When coding to work around this problem, I find both this->name and
>> base_class::name are ugly constructs. ?I don't use either to work
>> around this problem. ?Instead I use using directives, such as:
>>
>> template <class X>
>> class my_container : public std::vector<X>
>> {
>> ? ?typedef std::vector<X> super;
>> ? ?using super::size;
>> ? ?using super::empty;
>> ? ?typedef typename super::iterator iterator;
>>
>> At the start of most classes, I typedef the major base class as
>> "super" (not directly relevant to the current issue, but it helps).
>> Then I have the appropriate using super:: declarations to identify all
>> the function and variable names from that super class which will be
>> used bare in the current class. ?You can't use using declarations for
>> types from the base class, so those are best done with the typedef
>> typename construct.
>>
>>
>>
>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]