f



Why is this ambiguous?

Hi,

I ran below code with g++ 4.9.0 and get the following error.

amb.cpp: In member function ‘virtual void C::fnc(const A&)’:
amb.cpp:27:25: error: reference to ‘call’ is ambiguous
   void fnc( const A& ) { call( &IType<A>::fnc ); }
                          ^
amb.cpp:10:7: note: candidates are: template<class P> void 
Holder<T>::call(void (T::*)(P)) [with P = P; T = IType<B>]
   void call( void(T::*mp)(P) ) {}
        ^
amb.cpp:10:7: note:                 template<class P> void 
Holder<T>::call(void (T::*)(P)) [with P = P; T = IType<A>]


Why can't the compiler figure out which 'call' to use?

Thanks,
Daniel


class A {};
class B {};

template<class T>
class Holder : public T
{
protected:
    template<typename P>
    void call( void(T::*mp)(P) ) {}
};

// This works but above doesn't.
// template<class T, typename P>
// void call( void(T::*mp)(P) ) {}

template<typename T>
class IType
{
public:
    virtual void fnc( const T& ) = 0;
};

class C : public Holder<IType<A>>, public Holder<IType<B>>
{
public:
    void fnc( const A& ) { call( &IType<A>::fnc ); }
    void fnc( const B& ) { call( &IType<B>::fnc ); }
};

int main()
{
    return 0;
}


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
DeMarcus
9/3/2015 7:39:35 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

2 Replies
633 Views

Similar Articles

[PageSpeed] 20

{ edited by mod to shorten lines to ~70 characters. -mod }

On Thursday, September 3, 2015 at 8:40:17 AM UTC-4, DeMarcus wrote:
>
> Why can't the compiler figure out which 'call' to use?

The problem is because of member lookup rules as defined in Section 10.2/2

The following steps define the result of name lookup in a class scope,
C. First, every declaration for the name in the class and in each of
its base class sub-objects is considered. A member name f in one
sub-object B hides a member name f in a sub-object A if A is a base
class sub-object of B. Any declarations that are so hidden are
eliminated from consideration. Each of these declarations that was
introduced by a using-declaration is considered to be from each
sub-object of C that is of the type containing the declara-tion
designated by the using-declaration. If the resulting set of
declarations are not all from sub-objects of the same type, or the set
has a nonstatic member and includes members from distinct sub-objects,
there is an ambiguity and the program is ill-formed. Otherwise that
set is the result of the lookup.

This basically states that if you have the same name over separate
bases, EVEN IF they have different signatures, you have to specify the
base that you mean.

So change your C::fnc() to either:

    void fnc( const A& ) { Holder<IType<A>>::call( &IType<A>::fnc ); } 

or

    template <typename T>
    void fnc( const T& ) { Holder<IType<T>>::call( &IType<T>::fnc ); }

Cheers,


JAZ


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Jack
9/4/2015 7:53:18 AM
On Thursday, 3 September 2015 15:40:17 UTC+3, DeMarcus  wrote:
> Hi,
> 
> I ran below code with g++ 4.9.0 and get the following error.
> 
> amb.cpp: In member function 'virtual void C::fnc(const A&)':
> amb.cpp:27:25: error: reference to 'call' is ambiguous
>    void fnc( const A& ) { call( &IType<A>::fnc ); }
>                           ^
> amb.cpp:10:7: note: candidates are: template<class P> void 
> Holder<T>::call(void (T::*)(P)) [with P = P; T = IType<B>]
>    void call( void(T::*mp)(P) ) {}
>         ^
> amb.cpp:10:7: note:                 template<class P> void 
> Holder<T>::call(void (T::*)(P)) [with P = P; T = IType<A>]
> 
> 
> Why can't the compiler figure out which 'call' to use?

Because it is not required to. The 'Holder<IType<B>>::call' 
and 'Holder<IType<A>>::call' do not form overload set but
ambiguity and it may not resolve it.
 
> class A {};
> class B {};
> 
> template<class T>
> class Holder : public T
> {
> protected:
>     template<typename P>
>     void call( void(T::*mp)(P) ) {}
> };
> 
> // This works but above doesn't.
> // template<class T, typename P>
> // void call( void(T::*mp)(P) ) {}

This is just single free function template, there are
no overloads.

> 
> template<typename T>
> class IType
> {
> public:
>     virtual void fnc( const T& ) = 0;
> };
> 
> class C : public Holder<IType<A>>, public Holder<IType<B>>
> {
> public:

To make the 'call's proper overloads you can alias them in derived
class:
    
        using Holder<IType<A>>::call;
        using Holder<IType<B>>::call;

Alternatively you can explicitly qualify below with 
'Holder<IType<A>>::' or  'Holder<IType<B>>::'.
Pick your poison. ;-)

>     void fnc( const A& ) { call( &IType<A>::fnc ); }
>     void fnc( const B& ) { call( &IType<B>::fnc ); }
> };
> 
> int main()
> {
>     return 0;
> }


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
ISO
9/4/2015 9:40:19 AM
Reply:

Similar Artilces:

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta >...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

C/C++ Ambiguity in Order of Evaluation
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of parentheses. >From the reference manual I have :- 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators." 2) order of evaluation of operands of operators undefined except where noted. I was just told at another forum that v = u - ((u & -(u = 1)) << 1); has undefined behavior and the reason given was the order of evaluation of operands ...

C as a Subset of C++ (or C++ as a superset of C)
Isn't it a lame use of human time and effort to maintain completely separate C and C++ standards? As in the words of Betty White about Facebook: "It seems like an incredible waste of time". Why don't the two standards groups get together and agree on a common specification for the ground which both standards cover? There would still be two separate standards, but they'd both be exactly the same for the common ground. The common ground document could be referred to by both standards instead of being maintained by both groups in individual efforts resulting in...

c------->c++ and c++------>c
We can write c program in turbo c++ editor.But reverse is not true why? i.e The program will not compile if u write c++ program in turbo c editor vim wrote: > We can write c program in turbo c++ editor.But reverse is not true why? > i.e The program will not compile if u write c++ program in turbo c > editor This isn't about C; this is about some editor. Why the fircone should /we/ know? Ask in a relevant group. [And /why/, in an editor specialised to some specific programming languages, would you expect it to compile a /different/ one? Would you expect turbo perl to compi...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! Throne Software wrote: > Throne Software has opened up C, C++ and C# Forums at: > > http://www.thronesoftware.com/forum/ For all of your forums newsgroups already exist. > Join us! Why? Don't see any reason to support your marketing. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! In article <1120963701.862698.160260@g14g2000cwa.googlegroups.com>, Throne Software <mail@thronesoftware.com> writes >Throne Software has opened up C, C++ and C# Forums at: > >http://www.thronesoftware.com/forum/ > >Join us! Why? The beauty of news groups is that they come to me I don't have to go to them. Also all the people who have been around a while know what they are doing and are here. I noticed that there are some C and C++ Forums on another system...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! ...

C,, C++, Java, C#
I come from C,C++,Java and C#. What can SmallTalk do for me and for what cost? Regarding the Squeak thing, it's interesting though confusing. Is it an IDE? Why are the menus so wacky? "DM McGowan II" <nospam@nospam.net> wrote in message news:PLednX2vnLd11IncRVn-rA@comcast.com... > I come from C,C++,Java and C#. What can SmallTalk do for me ...? Ask not what Smalltalk can do for, but what can you do with Smalltalk. :-) In very few words, it significantly reduces time to market, while enhancig the development experience. This is because: 1. It...

c,c++,obj-c
Of the 3 languages titled in this post is c++ older than obj-c ? Would someone experienced in C be able to dive right into C++ rather than obj-c ? Java is out for me now except applet programming I think I'm going to go with just C and C++. Bill Bill Cunningham wrote: > Of the 3 languages titled in this post is c++ older than obj-c ? Why does that matter? Do you want to use an old language? Try Fortran or LISP. > Would > someone experienced in C be able to dive right into C++ rather than > obj-c ? Not quite dive in, at least not into the OO parts of C++. ...

my c lang.
my c language http://www.freewebs.com/medicsec/ http://bigchurch.com/go/g906803-pmem http://indianfriendfinder.com/go/g906803-pmem ...

Ambiguity about ambiguity
There's a well-known ambiguity in the SL grammar concerning arrays and texture channels: string mapnames[10]; string name = mapnames[2]; // 2 is an array subscript Ci = texture("map.tex"[3], s, t); // 3 is a texture channel Ci = texture(mapnames[2], s, t); // ambiguous - what is 2? The first mention of this ambiguity and how to resolve it came in the PRMan document "RenderMan Shading Language Extensions": [T]he existing square bracket notation in the texture call for channel number identification is syntactically ambiguo...

Web resources about - Why is this ambiguous? - comp.lang.c++.moderated

Category:Vague or ambiguous time from January 2008 - Wikipedia, the free encyclopedia
This is an administration category . It is used for administration of the Wikipedia project and is not part of the encyclopedia. It contains ...

Flickr: Clearly Ambiguous' Photostream
Where to start... - I find it very hard to write. - I love to take pictures. - Canon Rebel XT is my camera now. - Previous cameras included Sony ...

‘Limitless’ Star Jake McDorman on Bradley Cooper’s ‘Ambiguous’ Interest in His Character - YouTube
“This drug, NZT, has him in a chokehold and it won’t let go,” actor says while discussing CBS series during new episode of TheWrap’s “Drinking ...

Uber: Ambiguous advice to UberX drivers following GST deadline
&#65279;UberX drivers are affected by new tax rules that took effect at the weekend, but Uber has been less than clear in spelling out driver ...

Politicians' entitlements 'a bit ambiguous': Turnbull (01:22)
Malcolm Tunbull responds to reports the Treasurer Joe Hockey made several trips to his farm in Cairns on the taxpayers' dollar.

Tehran Taxi review: Iranian filmmaker Jafar Panahi produces ambiguous cab's eye view of capital city
Like so much Iranian cinema, Tehran Taxi inhabits an ambiguous zone where fiction and documentary mingle.

Politicians' entitlements 'a bit ambiguous': Turnbull (01:22)
Malcolm Tunbull responds to reports the Treasurer Joe Hockey made several trips to his farm in Cairns on the taxpayers' dollar.

Japan remains ambiguous over China-proposed AIIB, eyeing consultation with U.S.
Japan remains ambiguous over China-proposed AIIB, eyeing consultation with U.S.

Turkey’s ambiguous policies help terrorists join IS jihadist group: analyst - Global Times
A Chinese security analyst has claimed that the Turkish government’s ambiguous policies have allowed terrorists easy access to Turkey, enabling ...

Analysis: GCC 'ambiguous' about Russia in Syria
Although GCC states voiced opposition to Russia's intervention in Syria, they seek to build a new alliance with it.

Resources last updated: 1/25/2016 6:52:10 AM