473,385 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

using and ambiguous call

Hi,

is this legal c++ code?

template <typename BaseTstruct A
{
BaseT& this_() { return *static_cast<BaseT*>(this); }

template <typename Tvoid Foo() { this_().Bar<T>(); }
template <typename Tvoid Bar() { }
template <typename Tvoid Bar(i32) { }
};

struct B : public A<B>
{
using A<B>::Bar;

template <typename Tvoid Bar() { }
};
//..

B test; test.Foo<int>();

when using such a construction I got mixed results on various
compiler. Often I get an ambiguous call to overloaded function error
(A<B>::Bar() vs B::Bar()). Sometime it seem to make a difference when
using template member function or non-template member function.

If it is not legal, why not? Any ideas for good workarounds? I have
template member functions for which i want default implementations and
helpers/wrappers in the base class. Several directly from this base
class derived classes only implementing a subset of the default
implementation. Any ideas?
Jan 8 '08 #1
3 1593
va***@krautgames.de wrote:
is this legal c++ code?

template <typename BaseTstruct A
'BaseT' is a bad name. I would probably call it 'WrappedT'.
{
BaseT& this_() { return *static_cast<BaseT*>(this); }
This can have undefined behaviour if 'BaseT' isn't related to
'A<BaseT>', you realise that of course...
>
template <typename Tvoid Foo() { this_().Bar<T>(); }
It seems that you're missing the keyword 'template' here:

... { this_(). template Bar<T>(); }
.. ^^^^^^^^

but I can be wrong.
template <typename Tvoid Bar() { }
template <typename Tvoid Bar(i32) { }
};

struct B : public A<B>
{
using A<B>::Bar;

template <typename Tvoid Bar() { }
};
//..

B test; test.Foo<int>();

when using such a construction I got mixed results on various
compiler. Often I get an ambiguous call to overloaded function error
(A<B>::Bar() vs B::Bar()). Sometime it seem to make a difference when
using template member function or non-template member function.

If it is not legal, why not? Any ideas for good workarounds? I have
template member functions for which i want default implementations and
helpers/wrappers in the base class. Several directly from this base
class derived classes only implementing a subset of the default
implementation. Any ideas?
Try it with all those compilers after adding the keyword 'template',
although I am not sure how it should affect the outcome. It's just
a hunch.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 9 '08 #2
On Jan 9, 4:03 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
{
BaseT& this_() { return *static_cast<BaseT*>(this); }

This can have undefined behaviour if 'BaseT' isn't related to
'A<BaseT>', you realise that of course...
yes, but that's no problem for me. And this design/construction would
be extremely helpful if it would work :-(
template <typename Tvoid Foo() { this_().Bar<T>(); }

It seems that you're missing the keyword 'template' here:

... { this_(). template Bar<T>(); }
. ^^^^^^^^

but I can be wrong.
[...]
Try it with all those compilers after adding the keyword 'template',
although I am not sure how it should affect the outcome. It's just
a hunch.
doesn't change anything :-(

Any other ideas/workarounds? Any c++ standard gurus who can at least
verify if it should work?
Jan 9 '08 #3
On Jan 10, 10:14 am, James Kanze <james.ka...@gmail.comwrote:
Of the compilers I have available, two (g++ and Sun CC) have no
problems with your code, calling B::Bar(), as expected. VC++
seems to have problems with the using declaration, which results
in the ambiguity you mention. (It would seem that the compiler
is including all of the functions Bar from the base class,
rather than just those which aren't hidden.) Drop the using,
however, and it works. This is a bug, but the simple
work-around would be to drop the using, and also define Bar(i32)
in B.
Without all the member functions as templates it compiles on vc++,
too. But as vc++ is my main development platform it's really annoying.
The idea was to define default behaviour plus some wrappers in the
base class and only define a subset in several derived classes. When
dropping the using, the wrapper methods will not be resolved so again
copy-paste is needed which I wanted to prevent with this construction
in the first place :-)

Thanks for the help. At least I'm now convinced that this would be a
really nice pattern exactly fitting my needs and I have to see how to
convince the vc++ with a least worst workaround :-( well, nothing a
macro can't solve...
Jan 13 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
0
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
2
by: pvl_google | last post by:
Hi, I'm trying to extend an STL class with additional iterator functionality. In the simplified example below I added an extra iterator class with a dereferencing operator. This operator...
9
by: sebastian | last post by:
I've simplified the situation quite a bit, but essentially I have something like this: struct foo { }; void bar( foo const & lhs, foo const & rhs )
7
by: Ioannis Vranos | last post by:
In K&R2 errata page <http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html> there are some ambiguous errata, for which I propose solutions. Any comments are welcome. Ambiguous...
8
by: Anna Smidt | last post by:
lf.lfHeight = - (int) (fabs (pt.y) / 10.0 + 0.5) ; Error 1 error C2668: 'fabs' : ambiguous call to overloaded function ezfont.cpp 47 stasm I first thought that the compiler wants to...
32
by: Anna Smidt | last post by:
I am having an "ambiguous call to overloaded function" error again. This is the function: int nGetProfWidth (int ncols, unsigned ProfSpec) { if ((ProfSpec & PROF_2d) == 0) return ncols;...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.