1

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?

0

2 Answers 2

1

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;

    // ⋮
};
Sign up to request clarification or add additional context in comments.

3 Comments

/home/insights/insights.cpp:8:3: error: missing 'typename' prior to dependent type name 'T::value_type' T::value_type garbage_value; ^~~~~~~~~~~~~
@AmmarTamimi As the error message suggested, you need to have typename prior to T::value_type. Depends on the C++ version you are using, this syntax is required to hint T::value_type here is a type.
yes thanks, I fixed it last night, now the second code works not only with std::vector, but with std::string.
1

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

3 Comments

I thought: auto top_shelf{box}; will make "top_shelf" a vector<int> type not a "shelf" type.
@AmmarTamimi You can use shelf<decltype(box)> top_shelf{box};, then top_shelf will be "shelf" as you want. See my updated answer.
thanks it works, still it has some sort of redundancy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.