Reputation: 2977
I guess this information can be derived by taking a deep look inside the <list>
implementation, that's what I've done for the last day.
My problem is that I want a method of my template<class T>
(which have a private std::list<T>
member) to wrap...but I cannot figure out which return value give to this method.
My code look like this:
template <class T >
class MyTemplate {
std::list<T> myList;
...
somereturnvaluehere myMethod(){
return myList.begin();
}
...
}
Upvotes: 0
Views: 1003
Reputation: 15944
As others have said, it will be std::list<T>::iterator
. But in order to present a cleaner API, and allow yourself some flexibility to change this in the future, you may want to define your own typedef (that maps to std::list<T>::iterator
) and use that as the return type. I.e.:
template <class T >
class MyTemplate {
std::list<T> myList;
typedef std::list<T>::iterator iterator;
...
iterator myMethod(){
return myList.begin();
}
...
}
Here, I've named the typedef just iterator
, which means that users of your class will refer to it as MyTemplate<T>::iterator
-- but depending on what myMethod()
does and the details of your class, you might want to name it something more specific.
By using a typedef
, if you later change your mind and decide that you want to use a std::vector
rather than a std::list
, then your user's code should not need to be changed (though it will of course need to be recompiled).
Upvotes: 1
Reputation: 361322
It is either
typename std::list<T>::iterator
, (non-const version)typename std::list<T>::const_iterator
(const version)depending on whether myList
is a const
object or not, though in your case, it doesn't seem to be a const
object, which means you should use the first version, the non-const version.
In C++11, you don't need to worry about it, as if you want, you may use trailing-return type as:
auto myMethod() -> decltype(myList.begin())
{
return myList.begin();
}
But then I think, for your case, this is overkill, and in fact, makes things unnecessarily complicate than required. So I would suggest you not to use it; but just know that there is such a thing called trailing-return-type in C++11, whose usage may be justified in certain cases.
Upvotes: 8
Reputation: 227390
It is either
std::list<T>::iterator;
or
std::list<T>::const_iterator;
depending on whether your list is const
or not. You can use the type in your class like this:
struct Foo {
typedef typename std::list<SomeType>::iterator iterator;
typedef typename std::list<SomeType>::const_iterator const_iterator;
iterator foo() { return m_list.begin();}
const_iterator foo() const { return m_list.begin(); }
std::list<SomeType> m_list;
};
Upvotes: 3
Reputation: 10487
std::list<T>::begin()
will return you either a std::list<T>::iterator
or a std::list<T>::const_iterator
as others have said.
What's possibly more important is what are you trying to acheive from the myMethod()
function?
If you're simply writing a thin wrapper around a std::list
then I'm not sure I see the value. Ideally, you would encapsulate the list within your class so that its implementation is not exposed.
Upvotes: 1
Reputation: 92231
The list's begin
will return a std::list<T>::iterator
.
In your case it will be a dependent type, so somereturnvaluehere
should be
typename std::list<T>::iterator
Upvotes: 7