[introspection] if a function is not public

Hello all, Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public? class x { public: void f ( ) {}; protected: void g ( ) {}; private: void h ( ) {}; bool is_public(...) { ... } // some implementation void check ( ) { std::cout << is_public(x::f) << std::endl; // 1 std::cout << is_public(x::g) << std::endl; // 0 std::cout << is_public(x::h) << std::endl; // 0 } }; Thanks a lot! --Lorenzo

On 01.07.2011 0:40, Mathias Gaunard wrote:
On 06/30/2011 02:02 AM, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
I'm pretty sure there isn't.
..but if someone implement the C++ reflection framework + external tools (for automated generation of metadata) :), this sort of checking is trivially achievable

On Fri, Jul 1, 2011 at 11:20 AM, Max Sobolev <macsmr@ya.ru> wrote:
On 01.07.2011 0:40, Mathias Gaunard wrote:
On 06/30/2011 02:02 AM, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
I'm pretty sure there isn't.
..but if someone implement the C++ reflection framework + external tools (for automated generation of metadata) :), this sort of checking is trivially achievable
you mean like http://kifri.fri.uniza.sk/~chochlik/mirror-lib/html/ ? ;-)

On 01.07.2011 23:55, Matus Chochlik wrote:
On Fri, Jul 1, 2011 at 11:20 AM, Max Sobolev<macsmr@ya.ru> wrote:
On 01.07.2011 0:40, Mathias Gaunard wrote:
On 06/30/2011 02:02 AM, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
I'm pretty sure there isn't.
..but if someone implements the C++ reflection framework + external tools (for automated generation of metadata) :), this sort of checking is trivially achievable
you mean like http://kifri.fri.uniza.sk/~chochlik/mirror-lib/html/ ? ;-)
yeah

On 30/06/11 01:02, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
In C++0x the SFINAE rules have changed slightly so that inaccessible members are effectively the same as absent ones, so you could probably do something, but I don't think you have any recourse in C++03. John Bytheway

On 06/30/2011 11:24 PM, John Bytheway wrote:
On 30/06/11 01:02, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
In C++0x the SFINAE rules have changed slightly so that inaccessible members are effectively the same as absent ones, so you could probably do something, but I don't think you have any recourse in C++03.
Do compilers already follow this?

On 05/07/11 19:59, Mathias Gaunard wrote:
On 06/30/2011 11:24 PM, John Bytheway wrote:
On 30/06/11 01:02, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
In C++0x the SFINAE rules have changed slightly so that inaccessible members are effectively the same as absent ones, so you could probably do something, but I don't think you have any recourse in C++03.
Do compilers already follow this?
clang r132536 (3.0) does. g++ 4.5.2 doesn't. John

On 6/29/2011 8:02 PM, Lorenzo Caminiti wrote:
Hello all,
Is there any way (SFINAE, etc) to check at run-time or at compile-time if a member function is not public?
class x { public: void f ( ) {}; protected: void g ( ) {}; private: void h ( ) {};
bool is_public(...) { ... } // some implementation
void check ( ) { std::cout<< is_public(x::f)<< std::endl; // 1 std::cout<< is_public(x::g)<< std::endl; // 0 std::cout<< is_public(x::h)<< std::endl; // 0 } };
Thanks a lot! --Lorenzo _______________________________________________ Unsubscribe& other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
This is off the top of my head but... It might be possible by declaring a friend class which does the introspection ala my TTI generated classes. If the friend class can find the function but a non-friend class can not find the function, then one knows that the function is not public. Similarly if one could have an introspection class derived from the actual class being introspected and find the function while not finding it otherwise, one knows it is a protected function. Whether either of these things can actually be done by TTI I would have to take a much closer look at eventually. Of course in any case one would have to know that the actual function exists in the class else not finding the function in any situation could mean it does not exist at all. I know that Joel Falcou, who is the Review Manager for my TTI libary, played around with 'friend' in introspecting class data, but I never followed up looking at his idea when doing TTI. Your query gives me something to think about for TTI, although I think the actual usefulness might be pretty small even if it could be done even if you have found a use for it. BTW the review of TTI begins tomorrow, and any relevant idea is welcome even if I choose to not actually try to implement it ( it may also be impossible ).

On Thu, Jun 30, 2011 at 6:53 PM, Edward Diener <eldiener@tropicsoft.com> wrote:
This is off the top of my head but...
It might be possible by declaring a friend class which does the introspection ala my TTI generated classes. If the friend class can find the function but a non-friend class can not find the function, then one knows that the function is not public. Similarly if one could have an introspection class derived from the actual class being introspected and find the function while not finding it otherwise, one knows it is a protected function. Whether either of these things can actually be done by TTI I would have to take a much closer look at eventually. Of course in any case one would have to know that the actual function exists in the class else not finding the function in any situation could mean it does not exist at all.
This sounds interesting... when I have time I will play with friendship and SFINAE...
I know that Joel Falcou, who is the Review Manager for my TTI libary, played around with 'friend' in introspecting class data, but I never followed up looking at his idea when doing TTI. Your query gives me something to think about for TTI, although I think the actual usefulness might be pretty small even if it could be done even if you have found a use for it.
I agree. The use case I have for it is very domain specific and I don't think many other people would find this useful. Anyways, I will explain my use case. I need this feature because in Contract Programming class invariants should be checked only for public functions while they should not be checked by protected and private functions (see for example N1962). So I need to program something like "if this member function is public check the class invariants, else don't check". I actually only need to program this at run-time but I had to go all the way and add the public keyword to the function preprocessor signature so I could program this up at the pp level with a macro like: PP_IS_PUBLIC( public void (f) ( int x ) ) // expand to 1 PP_IS_PUBLIC( private void (f) ( int x ) ) // expand to 0 If there was a way to detect the function access level at compile-time (or even run-time) then I would not need to use pp-metaprogramming to do this check and I could simplify the above syntax by not requiring to specify the member function access level (which kind of look ugly).
BTW the review of TTI begins tomorrow, and any relevant idea is welcome even if I choose to not actually try to implement it ( it may also be impossible ).
I'll take a close look to TTI. Thanks. --Lorenzo
participants (6)
-
Edward Diener
-
John Bytheway
-
Lorenzo Caminiti
-
Mathias Gaunard
-
Matus Chochlik
-
Max Sobolev