Re: outer_class type trait?
Am 30.11.2011 21:48, schrieb kispaljr:
Is it possible to write a type trait template that can be used to get
the outer class of a nested class/enum?
No problem, just declare:
template<class T>
struct outer_class_of {};
template<class T>
struct is_nested_in : std::false_type {};
and require that people specialize these traits for their
outer-inner-type combinations.
I guess that is not what you meant, though. In the following I assume
you mean a fully automatic solution, so lets proceed:
I am thinking of something like this:
struct outer { struct inner {}; };
outer_class_of<inner>::type => would be a typedef for 'outer'
outer_class_of<outer>::type => would cause a non-existing member
compiler error (like enable_if)
I don't think that this can be automated. Note that this would clearly
conflict with name-lookup rules. If you as for
outer_class_of<inner>
inner will be looked in the current scope. So you would need to ask for
outer_class_of<outer::inner>
(assuming that outer is in the current scope). At this point we see that
this trait is not very helpful in general. It would only help, if
someone had created a typedef, like
typedef outer::inner io;
Now you like to know whether io has some parent type. This would
probably require compiler intrinsics.
or maybe something like:
is_nested_in<inner, outer>::value => true
is_nested_in<int, outer>::value => false
We have the same name-lookup issue as above.
I assume even if it is possible (I think it is maybe not) it would
require support from the compiler and the solution would not be
portable.
At the moment it would be fine with me, I just want to find a solution
that works with gcc 4.6.
If I correctly understand you, this does not work at all.
If it is not possible with the current standard, do you think it would
make sense to include it in the following versions?
No, if I understood you correctly.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]