help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "buggy"? behaviour of g++ with nested classes of a template class


From: 4zumanga
Subject: Re: "buggy"? behaviour of g++ with nested classes of a template class
Date: 18 May 2006 04:10:31 -0700
User-agent: G2/0.2

The problem is that the compiler internally doesn't actually try to
match "f(list<int>::iterator)", but actually "f(list_iterator<int>)",
or whatever type list<int>::iterator actually is. It isn't able to
figure out that this should match with list<T>::iterator.

You would be better just writing the function as:
template<T>
int f(T& a)
{ }

and then using iterator_traits to find out information about T. Yes
this means people could pass the "wrong object" to the function, but
that is unfortunatly unavoidable. If you really want to stop wrong
objects being passed, you could investigate enable_if, and do something
like (warning, this code will certainly not compile, but should give
you the vague idea)

template<T>
int f(T& a, enable_if<is_same<T, list<iterator_traits<T>::value_type>
>::value> = 0)
{ .. }

But I really really would just trust people to pass the right things to
the function. The other advantage of just allowing anything to be
passed is your code probably isn't actually list-specific, just
bidirectional iterator specific, so being more general is good.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]