Topic: Address of member in class scope
Author: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
  Date: Wed, 21 Mar 2007 18:34:34 CST Raw View
marwan.taher@gmail.com schrieb:
> Can someone tell me why the following code won't compile? I am taking
> the address of a class member within the class's declaration. This
> works for a pointer to member function, but fails for a pointer to
> data member. Is my compiler at fault? Or does the standard explicitly
> forbid this?
The standard does not explicitely forbid that. IMO the code
(commented or uncommented) is fine, therefore using
3.3.1/4 as reference, which says
"After the point of declaration of a class member, the member name
can be looked up in the scope of its class. [Note: this is true even
if
the class is an incomplete class. For example,
struct X {
enum E { z = 16 };
int b[X::z]; // OK
};
-end note]"
Additionally I also see no reasons why your example
should violate anyone of the requirements described
in 3.3.6 ([basic.scope.class]), especially not for
2) "A name N used in a class S shall refer to the same declaration
in its context and when re-evaluated in the completed scope of S.
No diagnostic is required for a violation of this rule."
or
3) "If reordering member declarations in a class yields an alternate
valid program under (1) and (2), the program is ill-formed, no
diagnostic is required."
I also observed that existing implementations differ here. E.g.
both Comeau and mingw 3.4 accept the code, but VS2005-SP1
rejects it (I guess you have tested the last one?).
Greetings from Bremen,
Daniel Kr   gler
---
[ 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.comeaucomputing.com/csc/faq.html                      ]
Author: "jam" <farid.mehrabi@gmail.com>
  Date: Thu, 22 Mar 2007 12:09:52 CST Raw View
On Mar 22, 4:34 am, "Daniel Kr   gler" <daniel.krueg...@googlemail.com>
wrote:
> marwan.ta...@gmail.com schrieb:
>
> > Can someone tell me why the following code won't compile? I am taking
> > the address of a class member within the class's declaration. This
> > works for a pointer to member function, but fails for a pointer to
> > data member. Is my compiler at fault? Or does the standard explicitly
> > forbid this?
>
> The standard does not explicitely forbid that. IMO the code
> (commented or uncommented) is fine, therefore using
> 3.3.1/4 as reference, which says
>
> "After the point of declaration of a class member, the member name
> can be looked up in the scope of its class. [Note: this is true even
> if
> the class is an incomplete class. For example,
> struct X {
> enum E { z = 16 };
> int b[X::z]; // OK};
>
> -end note]"
>
> Additionally I also see no reasons why your example
> should violate anyone of the requirements described
> in 3.3.6 ([basic.scope.class]), especially not for
>
> 2) "A name N used in a class S shall refer to the same declaration
> in its context and when re-evaluated in the completed scope of S.
> No diagnostic is required for a violation of this rule."
>
> or
>
> 3) "If reordering member declarations in a class yields an alternate
> valid program under (1) and (2), the program is ill-formed, no
> diagnostic is required."
>
> I also observed that existing implementations differ here. E.g.
> both Comeau and mingw 3.4 accept the code, but VS2005-SP1
> rejects it (I guess you have tested the last one?).
>
> Greetings from Bremen,
>
> Daniel Kr   gler
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-...@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html                     ]
multiple virtual inheitance postpones the calculation of data members`
address to runtime using dynamic typing and for this I guess that many
implementations assume all data member addresses to be evaluated at
runtime
---
[ 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.comeaucomputing.com/csc/faq.html                      ]
Author: "=?iso-8859-1?q?Daniel_Kr=FCgler?=" <daniel.kruegler@googlemail.com>
  Date: Fri, 23 Mar 2007 00:44:01 CST Raw View
jam schrieb:
> multiple virtual inheitance postpones the calculation of data members`
> address to runtime using dynamic typing and for this I guess that many
> implementations assume all data member addresses to be evaluated at
> runtime
1) According to 14.3.2/1 a pointer to member is a valid argument
for a non-type template.
2) According to 5.19/2+4 we have the concept of a "pointer to
member constant expression":
"A pointer to member constant expression shall be created using
the unary & operator applied to a qualifiedid operand (5.3.1),
optionally preceded by a pointer to member cast (5.2.9)."
which is one of the valid components of a constant expression
capable to initialize a non-local static object. Exactly this kind of
initialization belongs to the static initializations occuring
*before*
dynamic initialization takes place.
Both (1) and (2) are *static* accesses to a pointer of member
and exactly this kind has been performed by the OP. This has
nothing to do with possible dynamic typing. Similarily I can
aquire the static address of a pure, virtual member function,
although those are usually known as pure incarnations of
runtime entities:
struct V {
  virtual ~V() = 0;
  virtual void foo() = 0;
};
template < void (V::*)() >
struct Tester {};
int main() {
  Tester<&V::foo>(); // No problem -> Static access
}
(Btw.: The above program compiles successfully for all
three compiler's tested in my previous posting, *even*
for VS2005-SP1)
Greetings from Bremen,
Daniel Kr   gler
---
[ 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.comeaucomputing.com/csc/faq.html                      ]
Author: marwan.taher@gmail.com
  Date: Mon, 19 Mar 2007 11:41:49 CST Raw View
Can someone tell me why the following code won't compile? I am taking
the address of a class member within the class's declaration. This
works for a pointer to member function, but fails for a pointer to
data member. Is my compiler at fault? Or does the standard explicitly
forbid this?
struct Example
{
  // Template that takes a pointer to member function
  template<void (Example::*member)(int)>
    struct Command
    {
      void Set(Example& example, int value) { (example.*member)
(value); }
    };
  // Template that takes a pointer to data member
  template<int Example::*member>
    struct Property
    {
      void Set(Example& example, int value) { (example.*member) =
value; }
    };
  int value_;
  Example() : value_(0) {}
  int Get() const { return value_; }
  void Set(int value) { value_ = value; }
  // Can take an address of member function
  Command<&Example::Set> Setter;        // compiles...
  // Can NOT take an address of data member
  //Property<&Example::value_> Value;   // !!! WILL NOT COMPILE !!!
  // Can take an address of data member inside a function
  void SetValue(int value)
  {
    Property<&Example::value_> Value;   // compiles...
    Value.Set(*this, value);
  }
};
---
[ 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.comeaucomputing.com/csc/faq.html                      ]