Re: Virtual inheritance and typedef
"Trevor Vaughan" <clcppm-poster@this.is.invalid> wrote in message
news:op.u3ae9s0ixofjdi@trevor-pc...
Joe Smith <unknown_kev_cat@hotmail.com> wrote:
The problem is that you oversimplified your test case, and misinerpreted
the error message.
Here is there error message commeau C++ gives on the full version if I
omit
the "virtual":
"ComeauTest.c", line 88: error: "PolicySelector<Setter1, Setter2,
Setter3,
Setter4>::P3 [with Setter1=Policy3_is<CustomPolicy>,
Setter2=DefaultPolicyArgs, Setter3=DefaultPolicyArgs,
Setter4=DefaultPolicyArgs]" is ambiguous
Policies::P3::doPrint();
^
detected during instantiation of "void
BreadSlicer<PolicySetter1,
PolicySetter2, PolicySetter3, PolicySetter4>::print()
[with PolicySetter1=Policy3_is<CustomPolicy>,
PolicySetter2=DefaultPolicyArgs,
PolicySetter3=DefaultPolicyArgs,
PolicySetter4=DefaultPolicyArgs]" at line 108
That states the problem pretty clearly. Your problem is that without the
Virtual,
in BreadSlicer::print, there are two types named P3 in scope. I'm not
enough
of an expert to say why the virtual makes a difference, so I post below a
slight abridgment of the full code that still shows the issue.
Thanks a lot for clarifying the problem. For the record, here is the
compiler error I get from g++-4 when compiling your abridged version
of the full code:
../source/ComeauTest.cpp: In member function
'void BreadSlicer<PolicySetter3, PolicySetter4>::print()
[with PolicySetter3 = Policy3_is<CustomPolicy>,
PolicySetter4 = DefaultPolicyArgs]':
../source/ComeauTest.cpp:59: instantiated from here
../source/ComeauTest.cpp:42: error: no type named 'P3' in
'class PolicySelector<Policy3_is<CustomPolicy>, DefaultPolicyArgs>'
The diagnostic from the Comeau C++ compiler ("... P3 ... is ambiguous")
is certainly much more helpful than the "no type named 'P3'" I get
from g++!
Yuck.
I'm reporting this as a bug in g++, since that error message is so
misleading it is not even funny. This is now gcc bug 42021:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42021
If it helps, I shrunk this down to a 32 line test case that does not use
templates.
This may make the class relations more clear.
The issue at stake is why both C1 and C2 must use virtual inheritance to
avoid error.
----BEGIN---
struct Policy1 {
static void doPrint() {}
};
struct Policy2 {
static void doPrint() {}
};
struct B {
typedef Policy1 P3;
};
struct C1 : /*virtual*/ B {
typedef Policy2 P3;
};
struct C2 : /*virtual*/ B {
};
struct PolicySelector: C1, C2 {
};
struct BreadSlicer {
typedef PolicySelector Policies;
void print () {
Policies::P3::doPrint();
}
};
int main()
{
BreadSlicer bc2;
bc2.print();
}
---END---
Here are the errors each compile gives under this.
Visual C++ v8:
sourceFile.cpp(27) : error C2385: ambiguous access of 'P3'
could be the 'P3' in base 'C1'
or could be the 'P3' in base 'B'
(A suprisingly clear and helpful error message.)
Comeau/EDG:
"sourceFile.cpp", line 27: error: "PolicySelector::P3" is ambiguous
Policies::P3::doPrint();
^
G++:
sourceFile.cpp: In member function `void BreadSlicer::print()':
sourceFile.cpp:27: no type named `P3' in `struct PolicySelector'
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]