Topic: Fwd: pointer to member conversion to a pointer to member that is


Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Thu, 19 Aug 2004 15:50:28 GMT
Raw View
Hello,

Below is the message that has been posted to Microsoft language NGs and
discussed there.  I think it should be possible to change the language
to accommodate the conversion discussed.  Before a formal proposal is
finalised, could you please take a look and see if it has merit?

The basic idea is: if for two classes B and D exists an implicit
conversion from { D* to B* } (plain vanilla ptr-to-derived to ptr-to-base
conversion, see 4.10), then the conversion { D T::* to B T::* } for an
arbitrary class T should exist as well.  I there anything I am missing?

Thank you.

Victor

-------- Original Message --------
Subject: pointer to member conversion to a pointer to member that is a
member's base class
Date: Tue, 17 Aug 2004 13:43:49 -0700
From: Vladimir_petter <vladp72@hotmail.com>
Newsgroups: microsoft.public.dotnet.languages.vc,microsoft.public.vc.language

Dear All,

There is the problem in nutshells (see the program bellow):

It is ok to convert pointer to F<T> to the pointer to I.
Now if I have pointer to member "F<T> entity::*" can I convert it to the
"I entity::*"?
Compiler does not let me to do that (I've tried on VC 7.1 and Comeau 4.3.3).
If I do reinterpret_cast then the program will be compiled and runs as
expected all though I would like to know possible implications of this
approach.

The goal here to create a static array of fields for a class and be able to
execute algorithms on this array (like the for loop in the main function).

Thanks,
Vladimir.

#include <string>

using namespace std;

struct I
{
  virtual char const * foo()=0;
};

template <typename T>
struct F : public I
{
  char const * foo()
  {
   return typeid(T).name();
  }

  T v_;
};

struct entity
{
  typedef I entity::* mem_t;

  static mem_t fields[4];

  F<int>  f1_;
  F<char>  f2_;
  F<string> f3_;
  F<double> f4_;
};

entity::mem_t entity::fields[4] = {
  reinterpret_cast<mem_t>(&entity::f1_),
  reinterpret_cast<mem_t>(&entity::f2_),
  reinterpret_cast<mem_t>(&entity::f3_),
  reinterpret_cast<mem_t>(&entity::f4_),
};


int main(int, char **)
{
  entity e;
  for(int i=0; i<4; ++i)
  {
   printf("%s\n", (e.*(entity::fields[i])).foo());
  }

  return 0;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]