Topic: What are pointer to member constant expressions?
Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Sun, 4 Jul 2010 14:36:19 CST Raw View
In C++03 it says that a "&foo::bar" is a pointer to member constant
expression. Are template parameters of its type also constant expressions?
I'm asking because i have a template that has a non-type parameter of
pointer to member type, and it has a static member
template<int A::*p> struct foo {
static int A::*member;
};
template<int A::*p>
static int A::*foo<p>::member = p;
Now is the member "foo<p>::member" statically initialized, or is it
dynamically initialized? It seems to me, since the compiler knows "p" at
compile time, it would statically initialize. But the sections for constant
expressions doesn't mention template parameters. Is it implying they are
automatically so?
How do compilers treat that example? Thanks!
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.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: Tue, 6 Jul 2010 02:27:47 CST Raw View
On Jul 4, 10:36 pm, "Johannes Schaub (litb)" <schaub-johan...@web.de>
wrote:
> In C++03 it says that a "&foo::bar" is a pointer to member constant
> expression. Are template parameters of its type also constant expressions?
>
> I'm asking because i have a template that has a non-type parameter of
> pointer to member type, and it has a static member
>
> template<int A::*p> struct foo {
> static int A::*member;
>
> };
>
> template<int A::*p>
> static int A::*foo<p>::member = p;
>
> Now is the member "foo<p>::member" statically initialized, or is it
> dynamically initialized? It seems to me, since the compiler knows "p" at
> compile time, it would statically initialize. But the sections for constant
> expressions doesn't mention template parameters. Is it implying they are
> automatically so?
I don't think that this is implied somewhere else, so
by a strict reading of the C++03 wording there is no
guarantee for a static initialization in the scenario you
are describing. This is somewhat funny, though, since
the wording from 14.3.2 p.1 b. 2 already guarantees
that
class A;
template<int A::*> struct bar {};
template<int A::*p> struct foo { bar<p> m; };
is well-formed. I consider this as a defect in C++03.
> How do compilers treat that example?
Unfortunately I have no data available. But since your
example would be required to be a constant expression
in C++0x there are good chances that current C++03
compilers would already perform static initialization
here.
HTH & 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++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Simon Richter <Simon.Richter@hogyros.de>
Date: Tue, 6 Jul 2010 02:28:17 CST Raw View
On 2010=E5=B9=B407=E6=9C=8804=E6=97=A5 22:36, Johannes Schaub (litb) wrot=
e:
> In C++03 it says that a "&foo::bar" is a pointer to member constant
> expression. Are template parameters of its type also constant expressio=
ns?
Non-type template parameters need to be compile time constant
expressions as their value is mangled into the template instantiations'
names.
struct foo { char const *bar; };
typedef char const *(foo::* const pmt);
pmt one = &foo::bar;
template<pmt> class mytemplate { };
mytemplate<&foo::bar> tpl;
Here, "one" and "&foo::bar" are expressions of type "char const *(foo::* =
const)", that is, const pointer to data member inside "foo" of type
"char const *", however only the latter expression can be used as a
template parameter. "tpl" has the type "mytemplate<&(foo::bar)>". If it
were permissible to use "one" as a template parameter, the compiler
would need to resolve it back to "&foo::bar", in order to determine that =
multiple template instantiations are indeed the same type.
Inside the template declaration, the parameter can be used as a constant =
expression, so
template<pmt Mypmt>
struct myothertemplate : mytemplate<Mypmt> { };
is allowed.
> template<int A::*p> struct foo {
> static int A::*member;
> };
> template<int A::*p>
> static int A::*foo<p>::member = p;
That is a non-const static member with an initializer, thus it ends up
with all the other modifiable data, which is typically initialized
before statically allocated objects' constructors are run; whether that
is done by the loader or the startup code is implementation-defined but
should not matter. The only thing guaranteed from the standard is that
the static member is initialized before any ctors of (static or
nonstatic) foo<p> objects are run; that is, the foo ctor could assume p
in its own instantiation has been initialized.
If you were to declare that member const, it could be placed in the
read-only data or code section, depending on implementation, and
optimizations performed assuming that member remains constant (that is,
its instance could be omitted from the binary image and the value
substituted inline, as modification of that member via const_cast would
be UB), but again, that is implementation defined.
> Now is the member "foo<p>::member" statically initialized, or is it
> dynamically initialized?
Implementation defined.
> It seems to me, since the compiler knows "p" at
> compile time, it would statically initialize. But the sections for cons=
tant
>
> expressions doesn't mention template parameters. Is it implying they ar=
e
>
> automatically so?
The rules for template parameters are more strict than those for
constant expressions. The main reason why your code cannot be optimized
is that the pointer-to-member-object object is itself non-const.
Simon
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]