3

Short story:

struct A{};
struct B:private A{};

void f(void *){}

void f(A*){}

int main(){
 B* b;
 f(b);
}

and GCC complains error: ‘A’ is an inaccessible base of ‘B’

Long story: To see if a class is a subclass (or the same) of another without using boost, I do

template<typename B,typename D> struct is_base_or_same_of{

 typedef char (&yes)[2] ;  

 static yes test(const B* b);
 static char test(const void* p);

 static const D* d();

 static const bool value=sizeof(test(d()))==sizeof(yes);

};

and the situation is the same

How could I make the compiler "prefer" the void* version?

Fabio Dalla Libera
  • 1,297
  • 1
  • 10
  • 24
  • 1
    Welcome to StackOverflow! Unlike other sites, SO isn't a discussion forum, a request-and-response site, or a code-review site. SO is a question-and-answer forum. What is your question? – Robᵩ Mar 27 '12 at 16:51
  • 1
    It might be a good thing to just leave this question as is and try asking the *real* question: *How can I detect whether a type is a **visible** base of another type?* For what's worth, I don't know the answer. – David Rodríguez - dribeas Mar 27 '12 at 17:36
  • I asked it in a new thread, thank you http://stackoverflow.com/questions/9895262/how-can-i-detect-whether-a-type-is-a-visible-base-of-another-type – Fabio Dalla Libera Mar 27 '12 at 18:15

1 Answers1

2

This has to do with the process of overload resolution and how it goes. When you pass a pointer to the derived object, the compiler will try to find the best match for the function and determines that it is the overload that takes a pointer to A. Only then access specifiers are checked and the compiler complains that A is not an accessible base from that context.

The compiler is not allowed to go back, discard that overload and try with the rest of them.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
  • Thank you. Do you have any idea on why simply "not seeing" it's an A* is not done by the compiler when matching the signature? – Fabio Dalla Libera Mar 27 '12 at 17:09
  • @FabioDallaLibera: That is a design decision on the language, the standard defines that to be the behavior that compilers must take. – David Rodríguez - dribeas Mar 27 '12 at 17:34
  • 1
    @FabioDallaLibera: because The Standard says they can't. Overload resolution finds a best fit function without taking accessibility into account. If the best-fit function that is selected is inaccessible, "the program is ill-formed". (§13.3 in C++11 draft n3290) – Mat Mar 27 '12 at 17:34
  • @Mat :Thank you!! I report it here for the [ Note: The function selected by overload resolution is not guaranteed to be appropriate for the context. Other restrictions, such as the accessibility of the function, can make its use in the calling context ill-formed. — end note ] – Fabio Dalla Libera Mar 27 '12 at 17:48