473,793 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

constructor ambiguity

Hi

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why something does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
--------------------------------------------------------
template <typename Impl>
class Vector {};

template <typename Ref>
class VectorView : public Vector<VectorVi ew<Ref> >
{
public:
operator const Ref &() const { return _ref; }
private:
Ref _ref;
};

template <typename T>
class DenseVector : public Vector<DenseVec tor<T> >
{
public:
DenseVector() {}

template <typename Impl>
DenseVector(con st Vector<Impl> &rhs) {}

VectorView<Dens eVector<T> >
operator()(int from, int to) { return VectorView<Dens eVector<T>
(); }

};

int amin(const DenseVector<dou ble> &x) { return 1; }
int amin(const DenseVector<flo at> &x) { return -1; }

int main()
{
DenseVector<dou ble> x;
int i = amin(x(1,3));
}
--------------------------------------------------------
Some things are not really clear to me:

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<dou ble>. Why is any further
conversion considered as I have an exact matching function amin?

2) It's the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example. Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2> ?

best regards,
Alex
Oct 29 '05 #1
11 1870
* Alexander Stippler:

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why something does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
--------------------------------------------------------
template <typename Impl>
class Vector {};

template <typename Ref>
class VectorView : public Vector<VectorVi ew<Ref> >
{
public:
operator const Ref &() const { return _ref; }
private:
Ref _ref;
};

template <typename T>
class DenseVector : public Vector<DenseVec tor<T> >
{
public:
DenseVector() {}

template <typename Impl>
DenseVector(con st Vector<Impl> &rhs) {}

VectorView<Dens eVector<T> >
operator()(int from, int to) { return VectorView<Dens eVector<T>
(); } };

int amin(const DenseVector<dou ble> &x) { return 1; }
int amin(const DenseVector<flo at> &x) { return -1; }

int main()
{
DenseVector<dou ble> x;
int i = amin(x(1,3));
}
--------------------------------------------------------
Some things are not really clear to me:

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<dou ble>. Why is any further
conversion considered as I have an exact matching function amin?


Because the compiler must choose a conversion (you haven't specified
one), and there are three equally good candidates:

VectorView::ope rator const Ref() produces a DenseVector<dou ble>,

DenseVector<flo at>::DenseVecto r(x) produces a DenseVector<flo at>, and

DenseVector<dou ble>::DenseVect or(x) produces a DenseVector<dou ble>.

2) It's the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example.
Why? It works just fine.

Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2> ?


I don't think so. You cannot have it as both an implicit conversion,
and as a non-implicit conversion. If you absolutely must have it then
you'll have to choose. But you haven't said what problem it's intended
to solve. Perhaps the real problem admits some acceptable solution.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 29 '05 #2
Alexander Stippler wrote:
Hi

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my problem
(I at least learned, first to exactly consider why something does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
-------------------------------------------------------- template <typename Impl>
DenseVector(con st Vector<Impl> &rhs) {}

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<dou ble>. Why is any further
conversion considered as I have an exact matching function amin?

2) It's the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example. Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2> ?


Yes, the problem is that you have a constructor which allows implicit conversion from any
DenseVector with any template parameter to any other DenseVector with any other template
parameter. Another reason to avoid non trivial implicit constructors.

Why can not you make it explicit?

--

Valentin Samko - http://www.valentinsamko.com
Oct 29 '05 #3
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote:
Alexander Stippler wrote:
Hi

I have already posted and discussed the following problems once, but
despite really helpful hints I did not get any further with my
problem (I at least learned, first to exactly consider why something
does not
work instead of immediately searching for work arounds) . I have the
following code resulting in an ambiguity:
--------------------------------------------------------

template <typename Impl>
DenseVector(con st Vector<Impl> &rhs) {}

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<dou ble>. Why is any further
conversion considered as I have an exact matching function amin?

2) It's the templated constructor of DenseVector causing the trouble.
I cannot make it explicit, what would work in the example. Is there
another way to have this constructor, but prevent it from being
considered a candidate, if Impl is DenseVector<T2> ?


Yes, the problem is that you have a constructor which allows implicit
conversion from any DenseVector with any template parameter to any
other DenseVector with any other template parameter. Another reason
to avoid non trivial implicit constructors.

Why can not you make it explicit?

--

Valentin Samko - http://www.valentinsamko.com


In principle you're right. But for my purposes the 'assign notation'
for constructor is the natural way. What I would really like is a
mixture of explicit and implicit:
an explicit constructor, but allowing "Object A = something;" as
implicit initialization. But I found a workaround for my problem:
giving the constructor a second argument with default initialization
and restricting the availability of the constructor by SFINAE for the
second argument. Thus I can explicitely control for wich
instantiations the constructor is available and for which not.
Thanks for your help.

regards,
Alex
Oct 30 '05 #4
Alexander Stippler wrote:
In principle you're right. But for my purposes the 'assign notation'
for constructor is the natural way. What I would really like is a
mixture of explicit and implicit:
an explicit constructor, but allowing "Object A = something;" as
implicit initialization. But I found a workaround for my problem:
giving the constructor a second argument with default initialization
and restricting the availability of the constructor by SFINAE for the
second argument. Thus I can explicitely control for wich
instantiations the constructor is available and for which not.
Thanks for your help.


I still do not understand why you can not make your constructor explicit. You could still
have "Object A(something)".

--

Valentin Samko - http://www.valentinsamko.com
Oct 30 '05 #5
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote:
Alexander Stippler wrote:
In principle you're right. But for my purposes the 'assign notation'
for constructor is the natural way. What I would really like is a
mixture of explicit and implicit:
an explicit constructor, but allowing "Object A = something;" as
implicit initialization. But I found a workaround for my problem:
giving the constructor a second argument with default initialization
and restricting the availability of the constructor by SFINAE for the
second argument. Thus I can explicitely control for wich
instantiations the constructor is available and for which not.
Thanks for your help.


I still do not understand why you can not make your constructor
explicit. You could still have "Object A(something)".


Quite simple. It's a math library. And I want to have natural
notation like
Vector b = A*x-y
and not
Vector b(Ax-y).
This library is already used in lectures for students with no C++
knowledge at all. And then explain why you can write
Vector b; b = A*x-y;
but not
Vector b = A*x-y;
By the way. It is not only a nice notation, but also really efficient
since it prevents all unneccessary copying and temporaries.
Our other posting deals with this topic. We HEAVILY rely on
the copy constructor optimization the standard allows. And since C++
would be terribly slow if an implementation would not utilize this
optimization, we wonder why in some circumstances this optimization
is not done.

regards,
Alex
Oct 30 '05 #6
Alexander Stippler wrote:
I still do not understand why you can not make your constructor
explicit. You could still have "Object A(something)".
Quite simple. It's a math library. And I want to have natural
notation like
Vector b = A*x-y
and not
Vector b(Ax-y).

If it is a math library, I would expect high performance, and
Vector b = A*x - y;
is likely to be much slower than
Vector b = A;
b *= x;
x -= y;
as the first option may create two temporaries, and second option will not create any
temporaries.

Also, why do you need that constructor which accepts a template type at all? Why can't you
have a normal copy constructor?
This library is already used in lectures for students with no C++
knowledge at all. And then explain why you can write
Vector b; b = A*x-y;
but not
Vector b = A*x-y; You can definitely write this without your templated constructor as long as the type of
"A*x-y" is Vector.
By the way. It is not only a nice notation, but also really efficient
since it prevents all unneccessary copying and temporaries. How does it prevent all copying and temporaries? From what I see, it actually creates many
copies and temporaries (unless operator * returns a small proxy object, and you
overloaded operator - for that proxy object).
Our other posting deals with this topic. We HEAVILY rely on
the copy constructor optimization the standard allows. You can not really rely on NRVO/RVO, unless your library is not supposed to be used with
compilers which do not implement it.
And since C++
would be terribly slow if an implementation would not utilize this
optimization, we wonder why in some circumstances this optimization
is not done.

"C++ would be terribly slow" is a very generic comment. C++ is used for quite many years
without RVO/NRVO. By the way, even the newest compilers do not do RVO/NRVO if you return a
value from a function called in a function. For example:
struct A : public std::string { };
A foo() { A a; return a; }
int main() { A a = foo(); }
creates two objects of type A with VC++ 7.1, and only one with g++ 3.4, because g++
implements NRVO and VC++ does not.

But if we change "foo" to
A foo() { { A a; return a; } }
then you get a temporary with g++ as well.

--

Valentin Samko - http://val.samko.info
Oct 30 '05 #7

Alexander Stippler wrote:
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote:
Alexander Stippler wrote:
In principle you're right. But for my purposes the 'assign notation'
for constructor is the natural way. What I would really like is a
mixture of explicit and implicit:
an explicit constructor, but allowing "Object A = something;" as
implicit initialization. But I found a workaround for my problem:
giving the constructor a second argument with default initialization
and restricting the availability of the constructor by SFINAE for the
second argument. Thus I can explicitely control for wich
instantiations the constructor is available and for which not.
Thanks for your help.
I still do not understand why you can not make your constructor
explicit. You could still have "Object A(something)".


Quite simple. It's a math library. And I want to have natural
notation like
Vector b = A*x-y
and not
Vector b(Ax-y).
This library is already used in lectures for students with no C++
knowledge at all. And then explain why you can write
Vector b; b = A*x-y;
but not
Vector b = A*x-y;
By the way. It is not only a nice notation, but also really efficient
since it prevents all unneccessary copying and temporaries.
Our other posting deals with this topic. We HEAVILY rely on
the copy constructor optimization the standard allows. And since C++
would be terribly slow if an implementation would not utilize this
optimization, we wonder why in some circumstances this optimization
is not done.


http://osl.iu.edu/~tveldhui/papers/E.../exprtmpl.html
regards,
Alex


Oct 31 '05 #8
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote:
Alexander Stippler wrote:
I still do not understand why you can not make your constructor
explicit. You could still have "Object A(something)".
Quite simple. It's a math library. And I want to have natural
notation like
Vector b = A*x-y
and not
Vector b(Ax-y).

If it is a math library, I would expect high performance, and
Vector b = A*x - y;
is likely to be much slower than
Vector b = A;
b *= x;
x -= y;
as the first option may create two temporaries, and second option will
not create any temporaries.


We take the first approach, nevertheless the line will not produce any
temporary object. The second approach does an assignment and two
updates as separated operations. We map the line above to simply
storage allocation and one call to the appropriate underlying BLAS
routine. So temporaries are only generated if an expression can't be
mapped directly and has to be split into two or more (In some situations
there will be temporaries though a mapping to a BLAS
routine exists if there were a neccessity to reorder it to get a
matching - but optimizations will take place here in future).
Also, why do you need that constructor which accepts a template type
at all? Why can't you have a normal copy constructor?

Just to realize the mechanism used above.
This library is already used in lectures for students with no C++
knowledge at all. And then explain why you can write
Vector b; b = A*x-y;
but not
Vector b = A*x-y;

You can definitely write this without your templated constructor as
long as the type of "A*x-y" is Vector.


not if it has to be explicit.
By the way. It is not only a nice notation, but also really efficient
since it prevents all unneccessary copying and temporaries.

How does it prevent all copying and temporaries? From what I see, it
actually creates many copies and temporaries (unless operator *
returns a small proxy object, and you overloaded operator - for that
proxy object).


A user shall not see anything of it, correct. The feature we post
about is just another litte feature of the library. So no need
to deal with operator mapping here.
Our other posting deals with this topic. We HEAVILY rely on
the copy constructor optimization the standard allows.

You can not really rely on NRVO/RVO, unless your library is not
supposed to be used with compilers which do not implement it.
And since C++
would be terribly slow if an implementation would not utilize this
optimization, we wonder why in some circumstances this optimization
is not done.

"C++ would be terribly slow" is a very generic comment. C++ is used


In our case even one additional copy of a matrix is two much if
avoidable. We talk about large scale matrices and vectors.

for quite many years without RVO/NRVO. By the way, even the newest compilers do not do RVO/NRVO if you return a value from a function
called in a function. For example: struct A : public std::string { };
A foo() { A a; return a; }
int main() { A a = foo(); }
creates two objects of type A with VC++ 7.1, and only one with g++ 3.4,
because g++ implements NRVO and VC++ does not.

But if we change "foo" to
A foo() { { A a; return a; } }
then you get a temporary with g++ as well.

--

Valentin Samko - http://val.samko.info

Oct 31 '05 #9
In <11************ **********@g43g 2000cwa.googleg roups.com> Aleksey
Loginov wrote:

Alexander Stippler wrote:
In <43************ ***********@aut hen.white.readf reenews.net> Valentin
Samko wrote:
> Alexander Stippler wrote:
>
>> In principle you're right. But for my purposes the 'assign
>> notation' for constructor is the natural way. What I would really
>> like is a mixture of explicit and implicit: an explicit
>> constructor, but allowing "Object A = something;" as implicit
>> initialization. But I found a workaround for my problem: giving
>> the constructor a second argument with default initialization and
>> restricting the availability of the constructor by SFINAE for the
>> second argument. Thus I can explicitely control for wich
>> instantiations the constructor is available and for which not.
>> Thanks for your help.
>
> I still do not understand why you can not make your constructor
> explicit. You could still have "Object A(something)".
>


Quite simple. It's a math library. And I want to have natural
notation like
Vector b = A*x-y
and not
Vector b(Ax-y).
This library is already used in lectures for students with no C++
knowledge at all. And then explain why you can write
Vector b; b = A*x-y;
but not
Vector b = A*x-y;
By the way. It is not only a nice notation, but also really efficient
since it prevents all unneccessary copying and temporaries.
Our other posting deals with this topic. We HEAVILY rely on
the copy constructor optimization the standard allows. And since C++
would be terribly slow if an implementation would not utilize this
optimization, we wonder why in some circumstances this optimization
is not done.


http://osl.iu.edu/~tveldhui/papers/E.../exprtmpl.html


expression templates are a good choice in certain fields but have
their restrictions. Especially for large scale matrix-vector-products
they are IMHO not the best choice. And they are not flexible enough
for our purposes.
regards,
Alex


Oct 31 '05 #10

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

Similar topics

34
3116
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is this in any way used to create singletons. Can someone say how? Cheers, Andy
5
1510
by: Andy Buckley | last post by:
Hi, A friend and I have recently had trouble getting code to compile when using temporary objects in constructors. A minimal example is below: This code fragment is used to construct seven ROD objects and run their doSomething() method (which is trivial). As far as we can tell, all seven methods should successfully construct a ROD object using temporary objects as constructor parameters, but using
4
7134
by: Jeff Mallett | last post by:
VC++.NET gave me Compiler Error C2621, which states, "A union member cannot have a copy constructor." Yikes, that can't be true! As I understand it, *all* class objects have copy constructors, since if they aren't explicit, one is implicitly generated. If this were true, class objects could not be members of a union, but I know they can be. I assume what is meant is that a union member can't have
16
3760
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
8
4301
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
1
2650
by: Nathan Sokalski | last post by:
I have created a custom control for ASP.NET using VB.NET. My control inherits from the System.Web.UI.WebControls.CompositeControl class, and is working fine. However, the Visual Studio .NET designer shows the following error on the control in the designer: Error Creating Control - No parameterless constructor defined for this object I have defined four New methods. Although none of them are simply Public Sub New(), one of them has just one...
3
1946
by: Michael | last post by:
Hi All, why does this: class Base { protected: int var; public: Base():var(0){}; virtual int getVar() = 0;
1
1407
by: Bob Johnson | last post by:
Please note that this question is NOT about the merits of Microsoft certification (no need to get into that here). While taking a MS certification exam, I came across a question where it was obvious that two out of the five choices (a multiple choice question) would provide a workable solution to the scenario. The other chioces obviously incorrect. The *only* difference between the two alternatives is that they each used different...
12
7216
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? class Trial { public: Trial() {
0
9671
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
9518
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
10433
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...
1
10161
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
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
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.