473,802 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using and ambiguous call

Hi,

is this legal c++ code?

template <typename BaseTstruct A
{
BaseT& this_() { return *static_cast<Ba seT*>(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 1616
va***@krautgame s.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<Ba seT*>(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...@com Acast.netwrote:
{
BaseT& this_() { return *static_cast<Ba seT*>(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...@gm ail.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
2097
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. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
0
1697
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > > ============================================================== > Your code refuses to compile. In function 'foo<a>' 't' is a pointer to > const. Friendship has nothing to do with it. When I remove the attempt > to change 't->c' in foo<a>, it compiles and runs and displays > > T<a, 3>
1
10012
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 A, and the second one of type B. Let say, these are not my classes and I know nothing about the implementation. As system doesn't know what code must be used for resolving the language construction a+b (where A a; and B b;), it returns "The call...
0
278
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
2466
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 internally relies on the at function of the superclass. #include <vector>
9
2203
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
185
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 errata (mentioned first) of "The C Programming Language" 2nd Edition errata page (http://www-db-out.research.bell-labs.com/cm/cs/cbook/2ediffs.html), and
8
5070
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 fight with me because there where some decimals missing, but it's already .0 and .5. Also here an error "ambiguous call" is thrown...
32
2127
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; return int(sqrt(ncols)); //HERE THE ERROR IS THROWN }
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.