0

Consider a container A holding some data:

template<typename T>
struct A
{
    std::vector<T> data;
};

Inside, I will only store raw pointers, either const or not. Say:

A<int*> a1;
A<const int*> a2;

Now I need a member function that will return a reference on an element, because although I store pointers, I want the client code to use references (safer):

int&       b1 = a1.GetElem(0);
const int& b2 = a2.GetElem(0);

My question is: how do I get either a T& or const T& from that member function, depending on the stored elements.

What I have tried is to remove the pointer type, then add the reference:

std::add_lvalue_reference< std::remove_pointer<T>::type >::type
GetElem<T>( int i )
{
    return *data[i];
}

But this does not compile:

error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp> struct std::add_lvalue_reference’
note:   expected a type, got ‘std::remove_pointer<T>::type’

I also tried to use a typedef in my class, but this fails too:

typedef std::remove_pointer<typename T>::type T2;

Is there any way I can do that ?

Edit: The linked question asks about general advice on the usage of the typename keyword, while this one is about the specific problem of conversion between pointer type to reference type. This is reflected by the titles of the two questions, that are completely unrelated. Moreover, the linked question is only relevant to solve the given sample code error, but provides no solution to the question. If I hadn't given a code snippet, it would have never been classified as a dupe.

kebs
  • 6,387
  • 4
  • 41
  • 70
  • 2
    `typedef decltype(*declval())& reference;`. Though personally, I'd make `A` take the actual type, not a pointer, and have `std::vector data;` member. If you want clients to work in terms of `int` and not `int*`, then it would make more sense of them to use `A` and not `A – Igor Tandetnik Dec 17 '16 at 16:51
  • http://coliru.stacked-crooked.com/a/9ca3a2c95c32d42c – m.s. Dec 17 '16 at 16:52
  • @m.s. Thanks for your comment, it did the trick. But I don't agree with the dupe tag. Because although I agree that the solution is related, the question is not at all. I couldn't find it with my searches (yes, I did search before asking), because the title is completely unrelated, so it can't be considered as the same question. – kebs Dec 17 '16 at 17:07
  • @m.s. Would you mind posting your code as an answer? I have the feeling that others with the same problem will find it more easily than the other question, besides the fact that that other question's code if kinda obscure. – kebs Dec 17 '16 at 17:09
  • @IgorTandetnik Thanks for comment. The real code is somewhat more complex: it has to hold pointers because it points on data that is in other containers. But I don't want the client code to mess around with those pointers, thus the need for references. And both const and no const, because some client code might need to change data. – kebs Dec 17 '16 at 17:16
  • This is an exact duplicate to that question. for const, you need `std::add_const` – Danh Dec 19 '16 at 04:59

0 Answers0