1

I would like to use POD types as proxies for some structures. More precisely, I try to do the following thing:

struct Foo { /* some fields */ };

extern Foo global_array[SIZE]; // initialized elsewhere

struct Proxy
{
  Foo* pointer_;
}

Proxy get_first_element_as_pointee()
{ 
  return &global_array[0];
}

In the above code, I have a global C array containing Foo structures and I want to get the first one. However, I don't want to expose Foo but a Proxy. The problem is that the conversion does not work. I get the following error message:

error: could not convert ‘& global_array[0]’ from ‘Foo*’ to ‘Proxy’ { return &global_array[0]; }

EDIT: I modified my question so that it will be clearer

Aleph
  • 1,343
  • 1
  • 12
  • 27

1 Answers1

0

get_first_element_as_pointee() tries to return a Foo* but expects a Proxy. The compiler raises an error because it does not "know" how to convert Foo* to Proxy.

Although I admire the trick of Yola, mine is a bit different and simpler:

One possible way to "explain" is to define a conversion constructor for struct Proxy:

struct Foo { /* some fields */ };

extern Foo global_array[SIZE]; // initialized elsewhere

struct Proxy
{
  Foo* pointer_;

  // This is the trick: provide a "conversion constructor".
  Proxy(Foo *pointer): pointer_(pointer) { }
};

Proxy get_first_element_as_pointee()
{ 
  return &global_array[0];
}

Recalling the actual meaning of POD type (which was required in question title), this answer may not be sufficient.

According to this answer to SO: Can't C++ POD type have any constructor?:

POD means Plain Old Data type which by definition cannot have user-defined constructor.

Damn – my fault.

The other answer (without conversion constructor is even simpler) and was originally given by @bolov but deleted:

struct ProxyPOD
{
  Foo* pointer_;
};

ProxyPOD get_first_element_as_pointee2()
{ 
  ProxyPOD proxy = { &global_array[0] }; // as you would do in C
  return proxy;
}

life demo on ideone

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • @bolov Why post deleted? It was the third alternative which just came in my mind. – Scheff's Cat Feb 13 '18 at 16:03
  • Your solution suits well for general classes but it is more than expected for POD types as far as I understand POD types. Yola's solution is far better from a semantical viewpoint. – Aleph Feb 14 '18 at 10:37
  • @Aleph As VS2013 (where I do my daily work in) does not support initializers very well, I'm a little bit behind. As far as I understood it, Yola's `return {&global_array[0]};` is my second version (`get_first_element_as_pointee2()`) in modern, condensed form. – Scheff's Cat Feb 14 '18 at 13:07
  • Assuming that VS2013 does not support the solution, the one you proposed is not acceptable as it is not based on a POD type. As far as I know, your constructor is not necessary here as it could be replaced by a cast operator (overloading). Your first solution does not answer my question. – Aleph Feb 16 '18 at 10:56
  • @Aleph The first version (`struct Proxy`) is not as required - no POD type. I wrote this in text. The second version `struct ProxyPOD` is a POD type. The linked sample on ideone shows both versions (the wrong and the correct one). Sometimes an OP asks for certain things but realizes later that they were not relevant. (But I don't say it's the case here.) May be, I could've deleted the first part of answer but I took the effort to write it and hence, extended it only. May be, not the best decision... – Scheff's Cat Feb 16 '18 at 11:52
  • @Aleph Or did I get you wrong? `ProxyPOD proxy = { &global_array[0] };` is a C like initialization (i.e. it would work even if compiled in C). And, it compiles fine in VS2013 (C++) as I expected and just tested. Regarding C, I made a sample on [**ideone**](https://ideone.com/fCxlbC) (where I realized that I have to insert all the `struct`s to make it valid C code.) – Scheff's Cat Feb 16 '18 at 11:59
  • @Aleph When writing the above comments I didn't realize that _you are_ the OP. (just saw it afterwards.) So, you could write a [self-answer](https://stackoverflow.com/help/self-answer). It looks like Yola is not interested in the rep. you promised. (May be, because his rep. is already quite impressive...) ;-) – Scheff's Cat Feb 16 '18 at 12:35