Re: Help with pointer to data member template argument.
Corey O'Connor wrote:
Hello,
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. For example, I'm looking for a template 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) {}
};
I'd prefer the template to be generic to all types in a pointer to
member non-type argument. I've succeeded in getting something like I'd
want with:
template<typename T, typename V> no_type is_const_data_member_tester(V
T::*);
template<typename T, typename V> yes_type
is_const_data_member_tester(const V T::*);
but I haven't been able to work these into a template class such that
the template would fail to instantiate in the case that
is_const_data_member_tester would return no_type.
The template could be declared with a private constructor. A friend
function accepting a const member pointer parameter could instantiate
the class template, while a non-friend function accepting a non-const
member pointer could not - and the program would fail to compile in
that event:
template <class T, class M>
class DataMember
{
public:
// declare a friend factory function
template <class T1, class M1>
friend
DataMember<T1, M1>
GetDataMemberTemplate(const M1 T1::*mt);
private:
DataMember() {}
};
// factory function definition - requires const member pointer
template <class T, class M>
DataMember<T, M> GetDataMemberTemplate(const M T::*mt)
{
return DataMember<T, M>();
}
// non-const alternative causes compile-time error
template <class T, class M>
DataMember<T, M> GetDataMemberTemplate(M T::*mt)
{
return DataMember<T, M>(); // error - constructor private
}
struct A
{
int x;
A(int in_x) : x(in_x) {}
};
struct B
{
const int x;
B(int in_x) : x(in_x) {}
};
int main()
{
GetDataMemberTemplate(&A::x); // Error
GetDataMemberTemplate(&B::x);; // OK
}
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]