In C++ there are SFINAE tricks to get at compile-time if a class has a named field, typedef or method.
My question is: can we also somehow query if a method is accessible? Like distinguish between private and public methods.
For example if I have a class privately inheriting from std::vector
like class T : private std::vector<int> { ... };
I don't want to detect its begin
method since it's not publicly accessible.
When seraching arround I only get code to detect a method, nothing about visibility... The closest I got was this which addressed inherited methods but it's not clear how it interacts with visibility. Obviously I could test, but I'm concerned with compiler-specific behavior, since I'm working on a project on old Microsoft's compilers :\
Edit / clarification : It says here that the C++ standard changed its view on this issue: with C++03 any visible or invisible member is substitutable, but since C++11 only visible members are. Is this correct ?
I did some testing with the code below which detects the value_type
typedef. I'm not sure what the standard says, but I compiled with different compilers and here's the result:
- gcc : 4.4
true
| 4.5true
| 4.6true
| 4.7true
| 4.8false
| pre-4.9false
- Clang : 3.3
fail
- icc : 13
true
- msvc : 2005
true
| 2010true
| 2012true
Here's the code:
#include <vector>
typedef char yes;
typedef int no;
template<class T> struct has_value_type {
template<class U> static yes test(typename U::value_type*);
template<class U> static no test(...);
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
class C : private std::vector<int> { };
int main() {
return has_value_type<C>::value;
}