 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Corey O'Connor Guest
|
Posted: Sun Jul 30, 2006 9:59 pm Post subject: Help with pointer to data member template argument. |
|
|
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: but not &B: 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.
Any ideas?
-Corey O'Connor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Mon Jul 31, 2006 1:34 am Post subject: Re: Help with pointer to data member template argument. |
|
|
* Corey O'Connor:
| Quote: |
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.
| Quote: | For example, I'm looking for a template [function] that would fail to
instantiate for &A: but not &B: 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: ); // OK
bar( &A: ); // 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! ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Mon Jul 31, 2006 1:34 am Post subject: Re: Help with pointer to data member template argument. |
|
|
Corey O'Connor wrote:
| Quote: | 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: but not &B: 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: ); // Error
GetDataMemberTemplate(&B: );; // OK
}
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Corey O'Connor Guest
|
Posted: Mon Jul 31, 2006 6:23 am Post subject: Re: Help with pointer to data member template argument. |
|
|
Alf P. Steinbach wrote:
| Quote: | It would be interesting to hear why.
|
The reason is more of a learning experiment than anything practical. I
was originally think of how to design a std::set like container where
the aspect of the elements that determined their order was constant but
the rest of the elements were mutable. I figured developing a container
that verified this constraint under all possible conditions was likely
to be impossible in C++, but if the aspect that determined order was
assumed to be a data member of an object then it might be possible to
verify the data member was constant.
| Quote: | If you want to do it yourself, consider something like
....
But it's generally a good idea to check out the Boost library for such
things.
|
I agree, but for the purpose of learning I think developing this on my
own would teach me more than trying to read through the Boost library;
which is not very readable IMO. I suppose I could run the preprocessor
on the relevant sections, but I think that'd still get me code that
would be difficult to read in comparison to a more constraint approach.
Thanks for your help!
-Corey O'Connor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|