first code below compiled fine after sweating with the word 'class' five times in one line, and definition in "main" of shelf<std::vector, int> top_shelf; looks too fragmented to me, all of that just extract "class E : value_type" out of container.I needed this type_value so I can keep dummy:garbage_value in case an error out of range indexing through the container something like:
E& return_value_type_at(int x ){ return check(x)? (*container)[x] : garbage_value; }
here is the first code:
#include <iostream>
#include <vector>
template< template<class, class> class C, class E, class A = std::allocator<E> >
class shelf
{
C<E,A>* container;
// E garbage_value; // not completely implemented
public:
// bool check(x);
shelf(C<E,A>& x) : container{&x}{ }
E& return_value_type_at(int x ){ return /*check(x)?*/(*container)[x]/* : garbage_value*/; }
};
int main()
{
std::vector<int> box = {1,2,3,4};
shelf<std::vector,int> top_shelf{box};
return 0;
}
the second code below, compiled fine, looks a lot simpler:
#include <iostream>
#include <vector>
template<class T>
class shelf
{
T* container;
public:
shelf(T& x) : container{&x}{ }
auto& value_type_at(int x ){ return (*container)[x]; }
};
int main()
{
std::vector<int> box = {1,2,3,4};
shelf< std::vector<int> > top_shelf{box};
return 0;
}
here the keyword 'auto' helped me out because I have no clue what to replace it with, which is a problem in the same way, how am I going to do the "garbage_value"? another thing why not 'auto' here:
/home/insights/insights.cpp:16:9: error: 'auto' not allowed in template argument
shelf<auto> top_shelf{box};
^~~~
it makes a lot of sense: auto => 'box' template structure.
so is there a way to get "class E" out of the second code?
CodePudding user response:
auto
is not allowed to be passed as a template argument.
Instead of using shelf<auto>
you can instead use shelf<decltype(box)>
as shown below:
shelf<decltype(box)> top_shelf{box}; //works now
CodePudding user response:
If T
is the vector type, then you can get the value type by T::value_type
:
template<typename T>
class shelf
{
T::value_type garbage_value;
// ⋮
};