I try to implement the copy constructor of my std compatible custom iterator for a custom container.
The container looks something like this:
template <typename T, Alloc>
class container {
template <typename ValueType>
class raw_iterator;
...
using value_type = T;
...
using iterator = raw_iterator<value_type>
using const_iterator = raw_iterator<const value_type>
...
}
The raw_iterator looks something like this:
template <typename T, Alloc>
template <typename ValueType>
class container<T, Alloc>::raw_iterator {
...
}
If i'm right, I have to implement a copy constructor for both, the iterator and const_iterator, such that the iterators can be copied to the same type and a normal iterator can be copied into a const_iterator.
How can this be achieved?
PS: I have to provide some functionality in the copy constructor, so i cant use an implizit generated constructor.
You can create an implicit constructor for the const_iterator which takes an iterator as argument. Then everything will "just work".
Here's an example of what I think you want:
#include <type_traits>
template <typename ValueType>
class raw_iterator {
public:
// Typedefs
using non_const_value_type = std::remove_const_t<ValueType>;
using const_value_type = std::add_const_t<non_const_value_type>;
// Constructor - Need extra template argument here to allow SFINAE to work.
template <class U = ValueType,
std::enable_if_t<std::is_same<U, const_value_type>::value, int> = 0>
raw_iterator (raw_iterator<non_const_value_type> const & other) {
// Do stuff.
}
private:
// Friends - Make raw_iterator<X> friend of raw_iterator<X const>
friend std::conditional_t<
std::is_same<ValueType, non_const_value_type>::value,
raw_iterator<const_value_type>, void
>;
};
There's a conditional constructor taking an raw_iterator<AnythingNonConst> which only exists if the raw_iterator's own temple type is AnythingConst.
Furthermore, raw_iterator<AnythingNonConst> is a friend of raw_iterator<AnythingConst>, but not the other way around. So you can copy whatever members you want in the conditional constructor.
Here's an online example: https://wandbox.org/permlink/8gDzHyheIrpsTL5y.
Related
I want to create a template class which has an iterator of a STL container as a member. That is how far I got:
#include <iostream>
#include <vector>
using namespace std;
template<typename Element, template <class> class StdLibContainer>
struct ClassHoldingAnIteratorToAStandardContainer
{
ClassHoldingAnIteratorToAStandardContainer(){}
typename StdLibContainer<Element*>::iterator std_lib_iterator;
};
int main()
{
vector<int> vec{1,2,3};
ClassHoldingAnIteratorToAStandardContainer<int,vector<int>> holding_iterator_to_vec;
//DOES NOT WORK, compiler says: expected a class template, got ‘std::vector<int>’
return 0;
}
Could you explain the syntax template <typename> class StdLibContainer?
I found it somewhere on stackoverflow. BUt I don't understand it.
How can I create an instance of ClassHoldingAnIteratorToAStandardContainer ? All my attempts failed so far. The compiler always gives the error message: `expected a class template, got ‘std::vector’
In the above example i want to assign holding_iterator_to_vec vec.begin().
template <typename> class is the same as template <class> class. Originally, when templates were introduced, they allowed two equivalent forms:
template<class T> struct Foo {};
// or
template<typename T> struct Foo {};
Do not ask me why! However, the same was not true for template template parameters:
template <template <class> typename T> struct Foo {};
was the only allowed syntax. Apparently, people were unhappy about it, so the syntax was relaxed.
As for your second question, std::vector takes at least two template arguments, data type and allocator. This is why a single argument template doesn't cut it before C++17. After C++17, it would work.
To make it universal, use
template<template <class...> class Container> struct Foo{};
Unless you really need to know the type of the container, I would strongly recommend to keep your ClassHoldingAnIteratorToAStandardContainer independent of the concrete container type. If you just need the iterator, this is simpler and sufficient:
template<typename iterator>
struct iterator_wrapper {
iterator iter;
};
Thats the minimum you need to have an iterator as member :).
I dont really know what you want to use the iterator for, so just for the sake of an example lets add methods that actually use the iterator....
#include <iterator>
#include <vector>
#include <iostream>
template<typename iterator>
struct iterator_wrapper {
using value_type = typename std::iterator_traits<iterator>::value_type;
iterator iter;
bool operator!=(const iterator& other) { return iter != other;}
iterator_wrapper& operator++(){
++iter;
return *this;
}
const value_type& operator*() { return *iter; }
};
template <typename iterator>
iterator_wrapper<iterator> wrap_iterator(iterator it) {
return {it};
}
int main() {
std::vector<int> vec{1,2,3};
auto it = wrap_iterator(vec.begin());
for (;it != vec.end();++it) std::cout << *it;
}
Also there is a problem in your code.
typename StdLibContainer<Element*>::iterator
is for containers of pointers while in main you have ints. If you want to infer the iterator type from the container type then you can do it for example like this:
template <typename container,
typename iterator = typename container::iterator>
iterator_wrapper<iterator> wrap_begin(container& c) {
return {c.begin()};
}
which makes creating an iterator_wrapper as simple as
auto x = wrap_begin(vec);
Note that this answer applies to C++11, in newer standards there are deduction guides that make such make_x methods more or less superfluous afaik.
I'm now working on implementing a standard, and there is a template class vector_class defined in the specification. I just used alias template
template <class T, class Allocator=std::allocator<T>>
using vector_class = std::vector<T, Allocator>;
In the later work, I have a function call vector_class::data() which returns a pointer with the type of T*.
Everything works fine except T is bool. As you all know, std::vector<bool> is a possibly space-efficient specialization of std::vector for the type bool, and it don't implement the member function data, and actually the return type of vector<bool>::data() on my machine is void. Now here is the problem, we have some code like:
template <class T>
class A {
public:
vector_class<T> buffer;
T* ptr; // this pointer is defined in the specification thus it is indispensable
A(T* data, size_t size) {
buffer.resize(size);
ptr = buffer.data();
std::copy(data, data + size, ptr);
}
};
If the T is bool, the compiler will raise an error that cannot convert the type void to bool* in the code ptr = buffer.data().
Well, for my current implementation, it is the last option to avoid using std::vector but the alternative in Boost. What I expect is something like partial specialization of alias templates but unluckily it is not allowed according to the C++ standard. Therefore, I want to ask, if there any other way to deal with such problem?
You can make a partial specialization of proxy class to be used with alias template:
template<typename T, typename Allocator> class
vector_class_impl final
{
public: using type = std::vector<T, Allocator>;
};
template<typename Allocator> class
vector_class_impl<bool, Allocator> final
{
public: using type = something_else<bool, Allocator>;
};
template <typename T, typename Allocator = std::allocator<T>>
using vector_class = typename vector_class_impl<T, Allocator>::type;
This probably can't be helped, so you'll need to do slightly more work:
template <class T, class Allocator=std::allocator<T>>
struct vector_class: std::vector<T, Allocator>
{
using std::vector<T, Allocator>::vector;
// and member types too
};
template<class Allocator>
struct vector_class<bool, Allocator>
{
// recreate the whole vector interface here
};
I think you can specialize whole class A template for bool, replacing the vector_bool<T> with something else like boost::container::vector<T>.
I wrote a template that wraps a std::vector to make sure the vector is always sorted:
template <typename T> class SortedVector{
public:
SortedVector(bool (*comparator)(T,T)=DefaultComparator<T>){
this->comparator = comparator;
}
void insertValue(T newElement){
vect.insert(std::lower_bound(
vect.begin(),vect.end(),newElement,comparator),newElement);
}
private:
std::vector<T> vect;
bool (*comparator)(T,T);
};
I want to be able to use a custom comparators, but in most cases it will be ok to simply use T's < operator. However, I did not find any better way than to use this
template <typename T> bool DefaultComparator(T a,T b){return a<b;}
as the default parameter.
Maybe it is a stupid question... Isn't there a nicer way to get the same without defining my own DefaultComparator?
I cannot use C++11.
There isn't a standard function template to do what you want; but there is a standard function class template, std::less. If you were to use a generic function object, rather than restricting yourself to a function pointer, then it's easy to specify that as a default:
template <typename T, typename Comparator = std::less<T> >
class SortedVector{
public:
SortedVector(Comparator comparator = Comparator()){
this->comparator = comparator;
}
void insertValue(T newElement){
// lower_bound accepts any suitable function object, so no change needed
vect.insert(std::lower_bound(
vect.begin(),vect.end(),newElement,comparator),newElement);
}
private:
std::vector<T> vect;
Comparator comparator;
};
You can use class template std::less<T> as default.
template <typename T, typename C=std::less<T>>
class SortedVector
{
public:
SortedVector(C cmp=C()) : comparator(cmp)
{
}
void insertValue(T newElement){
vect.insert(std::lower_bound(
vect.begin(),vect.end(),newElement,comparator),newElement);
}
private:
std::vector<T> vect;
C comparator;
};
I created a read only iterator which allows me to use it in a for loop more conveniently then with the std iterators, similar to what boost does with the FOREACH (not as good but good enough :))
Now it looks like this:
for(ReadOnylIterator<MyClass *> class = parent.getIterator(); class.end(); ++class)
class->function();
The problem that I have now is, that I must implement a function on the parent which returns the iterator. Since the std containers have all the same syntax, I was wondering if it is possible to define a copy constructor/assignment operator on the Iterator that accepts any of the std:: containers and creates the copy itself, instead of requiring the class to return it.
Of course I want to avoid having to define all of them myself like as there are lots of them:
ReadOnlyIterator<T> &operator=(std::list<T> const &v)
ReadOnlyIterator<T> &operator=(std::vector<T> const &v)
...
Is there a way to do this? When I look at the source of the vector I don't see a common base class, so I think it might not be posssible.
I don't understnad why the assignment operator doesn't work.
In my code I test it like this:
std::vector<SimpleClass *>t;
ReadOnlyIterator<SimpleClass *> &it = t;
And I get
error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'ReadOnlyIterator<T> &'
If I understand you correctly, this should work:
#include <type_traits>
template <typename Container>
typename std::enable_if<std::is_same<T, typename Container::value_type>::value, ReadOnlyIterator<T>&>::type operator= (const Container &v);
The above code uses C++11. If you don't have access to that, you could use the equivalent functionality from Boost.
Live example
In case you can use neither C++11 nor Boost, you can code the necessary classes yourself:
template <typename T, typename U>
struct is_same
{
enum { value = 0 };
};
template <typename T>
struct is_same<T, T>
{
enum { value = 1 };
};
template <bool, typename>
struct enable_if
{};
template <typename T>
struct enable_if<true, T>
{
typedef T type;
};
To use this in a constructor, define it like this:
template <typename Container>
ReadOnlyIterator(const Container &v, typename enable_if<is_same<T, typename Container::value_type>::value, void>::type * = 0) {}
Live example
I'm sorry but no, this is not possible given that the containers don't have common base classes. You'll have to write an operator for each container, Sad news !
Although if the behavior of the operator functions is always the same, you could just make a macro that contains the code, and copy paste it for every container type ^_^
The following code represents a container based on std::vector
template <typename Item>
struct TList
{
typedef std::vector <Item> Type;
};
template <typename Item>
class List
{
private
typename TList <Item>::Type items;
....
}
int main()
{
List <Object> list;
}
Is it possible to templatize std::vector and create a general container, something like that?
template <typename Item, typename stl_container>
struct TList
{
typedef stl_container<Item>;
};
where stl_container represents std::vector, std::list, std::set...? I would like to choose the type of container at the time of the creation.
List <Object, std::vector> list; //vector of objects, not a real code
List <Object, std::vector> list; //list of objects, not a real code
Thanks for your answers...
Updated question:
I tried the following code but there are errors:
#include <vector>
template <typename Item, typename Container>
struct TList
{
typedef typename Container <Item>::type type; //Error C2059: syntax error : '<', Error C2238: unexpected token(s) preceding ';
};
template <typename T>
struct vector_container
{
typedef std::vector<T> type;
};
int _tmain(int argc, _TCHAR* argv[])
{
TList <int, vector_container> v;
TList <int, map_container> m;
}
Yes, but not directly:
template <typename Item, template <typename> class Container>
struct TList
{
typedef typename Container<Item>::type type;
};
Then you can define different container policies:
template <typename T>
struct vector_container
{
typedef std::vector<T> type;
};
template <typename T>
struct map_container
{
typedef std::map<T, std::string> type;
};
TList<int, vector_container> v;
TList<int, map_container> m;
A bit verbose, though.* To do things directly, you'd need to take the route described by James, but as he notes this is ultimately very inflexible.
However, with C++0x we can do this just fine:
#include <map>
#include <vector>
template <typename Item,
template <typename...> class Container, typename... Args>
struct TList
{
// Args lets the user specify additional explicit template arguments
Container<Item, Args...> storage;
};
int main()
{
TList<int, std::vector> v;
TList<int, std::map, float> m;
}
Perfect. Unfortunately there's no way to reproduce this in C++03, except via the indirection policy classes introduce as described above.
*I want to emphasize that by "A bit verbose" I mean "this is unorthodox". The correct solution for your problem is what the standard library does, as Jerry explains. You just let the user of your container adapter specify the entire container type directly:
template <typename Item, typename Container = std::vector<Item>>
struct TList
{};
But this leaves a big problem: what if I don't want the value type of the container to be Item but something_else<Item>? In other words, how can I change the value type of an existing container to something else? In your case you don't, so read no further, but in the case we do, we want to rebind a container.
Unfortunately for us, the containers don't have this functionality, though allocators do:
template <typename T>
struct allocator
{
template <typename U>
struct rebind
{
typedef allocator<U> type;
};
// ...
};
This allows us to get an allocator<U> given an allocator<T>. How can we do the same for containers without this intrusive utility? In C++0x, it's easy:
template <typename T, typename Container>
struct rebind; // not defined
template <typename T, typename Container, typename... Args>
struct rebind<T, Container<Args...>>
{
// assumes the rest are filled with defaults**
typedef Container<T> type;
};
Given std::vector<int>, we can perform rebind<float, std::vector<int>>::type, for example. Unlike the previous C++0x solution, this one can be emulated in C++03 with macros and iteration..
**Note this mechanism can be made much more powerful, like specifying which arguments to keep, which to rebind, which to rebind themselves before using as arguments, etc., but that's left as an exercise for the reader. :)
I'm a bit puzzled why some very smart (and competent) people are saying no.
Unless I've misread your question, what you're trying to accomplish is virtually identical to the "container adapters" in the standard library. Each provides an interface to some underlying container type, with the container type that will be used provided as a template parameter (with a default value).
For example, a std::stack uses some other container (e.g., std::deque, std::list or std::vector) to hold the objects, and std::stack itself just provides a simplified/restricted interface for when you just want to use stack operations. The underlying container that will be used by the std::stack is provided as a template parameter. Here's how the code looks in the standard:
namespace std {
template <class T, class Container = deque<T> >
class stack {
public:
typedef typename Container::value_type value_type;
typedef typename Container::size_type size_type;
typedef Container container_type;
protected:
Container c;
public:
explicit stack(const Container& = Container());
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
value_type& top() { return c.back(); }
const value_type& top() const { return c.back(); }
void push(const value_type& x) { c.push_back(x); }
void pop() { c.pop_back(); }
};
}
Of course, perhaps I've just misunderstood the question -- if so, I apologize in advance to the people with whom I'm (sort of) disagreeing.
Yes and no.
You can use a template template parameter, e.g.,
template <typename Item, template <typename> class Container>
struct TList { /* ... */ };
However, in general, template template parameters are not particularly useful because the number and types of the template parameters have to match. So, the above would not match std::vector because it actually has two template parameters: one for the value type and one for the allocator. A template template parameter can't take advantage of any default template arguments.
To be able to use the std::vector template as an argument, TList would have to be declared as:
template <typename Item, template <typename, typename> class Container>
struct TList { /* ... */ };
However, with this template, you wouldn't be able to use the std::map template as an argument because it has four template parameters: the key and value types, the allocator type, and the comparator type.
Usually it is much easier to avoid template template parameters because of this inflexibility.
well, you can hack it up with a macro:
template <typename T, typename stl_container = std::vector<T> >
struct TList
{
typedef stl_container Type;
};
#define TLIST(T, C) TList<T, C<T> >
TList<int> foo;
TList<int, std::list<int> > bar;
TLIST(int, std::list) baz;
Is it possible to templatize std::vector and create a general container, something like that?
No. You would have to templatize the function or object using the container -- you couldn't templatize the container itself.
For example. consider a typical std::find:
template<class InputIterator, class T>
InputIterator find ( InputIterator first, InputIterator last, const T& value )
{
for ( ;first!=last; first++) if ( *first==value ) break;
return first;
}
This works for any container, but doesn't need a tempalte with the container at all.
Also, given that it looks what you're trying to do is make container independent code, you might want to buy or borrow yourself a copy of Scott Meyers' Effective STL and read Item 2: Beware the illusion of container-independent code.
You can use template template parameters as others have mentioned here. The main difficulty with this is not that dissimilar container types have dissimilar template parameters, but that the standard allows the standard containers, like vector, to have template parameters in addition to the documented, necessary ones.
You can get around this by providing your own subclass types that accept the appropriate template parameters and let any extras (which have to have defaults) be filled in my the implementation:
template < typename T > struct simple_vector : std::vector<T> {};
Or you can use the templated typedef idiom:
template < typename T > struct retrieve_vector { typedef std::vector<T> type; };