Re: Help with pointer to data member template argument.
* Corey O'Connor:
I'm trying to figure out how to make a template that will fail to
instantiate if given a pointer to data member argument with a non-const
type.
It would be interesting to hear why.
For example, I'm looking for a template [function] that would fail to
instantiate for &A::x but not &B::x with A and B defined like so:
struct A
{
int x;
A(int in_x) : x(in_x) {}
};
struct B
{
const int x;
B(int in_x) : x(in_x) {}
};
If you want to do it yourself, consider something like
template< typename MemPtr >
struct PointerToConst
{
static MemPtr instance();
enum{
ok = (sizeof( isConstPtr( instance() ) ) == sizeof(Yes))
};
typedef typename MustBeTrue<void, ok>::T VoidType;
};
template< typename MemPtr >
typename PointerToConst<MemPtr>::VoidType bar( MemPtr f ) {}
int main()
{
bar( &B::x ); // OK
bar( &A::x ); // Fail
}
But it's generally a good idea to check out the Boost library for such
things.
At least with the compiler I tried this on, the above produced a small
avalanche of error messages for the failure case; the Boost error
reporting is probably much better.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]