Re: Template metafunction returning pointer-to-member
On Sep 26, 3:50 am, Marsh Ray <marsh...@gmail.com> wrote:
On Sep 25, 4:31 pm, Yechezkel Mett <ymett.on.use...@gmail.com> wrote:
It's nothing to do with the ptr-to-member; the same thing would happen
with any type. Try the following:
float sample::* const choose_p2mem<sample, 1>::ptr2mem =
&sample::member_one;
Thanks, that was part of my compile error, but I'm not much closer to
my goal. It seems I can provide types and integral constant
expressions
as compile-time constants from my template, but not pointer-to-
members.
....
So it seems I can take ptr-to-mems as non type template arguments,
and even specialize on them, but cannot publish them from structs
as compile-time constants. I don't see an obvious reason for this
limitation. Perhaps it's just one of those little corners below
the horizon of the vendors and standards group.
Will I be able to do this in C++ 0x?
I would have expected some use of constexpr to allow this, but looking
at the draft C++0x standard (N2588) I see the following:
"""
14.3.2 Template non-type arguments [temp.arg.nontype]
1 A template-argument for a non-type, non-template template-parameter
shall be one of:
? an integral constant expression; or
? the name of a non-type template-parameter; or
? the address of an object or function with external linkage,
including function templates and function template-ids but excluding
non-static class members, expressed as & id-expression where the & is
optional if the name refers to a function or array, or if the
corresponding template-parameter is a reference; or
? a constant expression that evaluates to a null pointer value (4.10);
or
? a constant expression that evaluates to a null member pointer value
(4.11); or
? a pointer to member expressed as described in 5.3.1.
"""
Note that the fourth and fifth items allow constant expressions for
pointers and pointer-to-members only if they evaluate to null. The
third and sixth items allow pointers and pointers to members if they
are of the form &object or &class::member, but no other expressions.
So it seems that there is no way to do it.
Any other ideas?
Depending on what you are trying to do you might be able to invert the
logic. For your original example, you can do something like this:
struct sample
{
float member_one;
};
template<class T, int N, template<float T::*> class F>
struct choose_and_apply_p2m;
template<template<float sample::*> class F>
struct choose_and_apply_p2m<sample, 1, F>
{
typedef F<&sample::member_one> type;
};
template<float sample::* p>
struct is_member_one
{
static const bool value = false;
};
template<>
struct is_member_one<&sample::member_one>
{
static const bool value = true;
};
void f()
{
BOOST_STATIC_ASSERT((choose_and_apply_p2m<sample, 1,
is_member_one>::type::value));
}
Yechezkel Mett
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]