473,399 Members | 2,159 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,399 software developers and data experts.

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<VectorView<Ref> >
{
public:
operator const Ref &() const { return _ref; }
private:
Ref _ref;
};

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

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

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

};

int amin(const DenseVector<double> &x) { return 1; }
int amin(const DenseVector<float> &x) { return -1; }

int main()
{
DenseVector<double> 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<double>. 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 1837
* 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<VectorView<Ref> >
{
public:
operator const Ref &() const { return _ref; }
private:
Ref _ref;
};

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

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

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

int amin(const DenseVector<double> &x) { return 1; }
int amin(const DenseVector<float> &x) { return -1; }

int main()
{
DenseVector<double> 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<double>. 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::operator const Ref() produces a DenseVector<double>,

DenseVector<float>::DenseVector(x) produces a DenseVector<float>, and

DenseVector<double>::DenseVector(x) produces a DenseVector<double>.

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(const Vector<Impl> &rhs) {}

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<double>. 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***********************@authen.white.readfreene ws.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(const Vector<Impl> &rhs) {}

1) what causes the ambiguity. The conversion operator in
class VectorView returns a DenseVector<double>. 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***********************@authen.white.readfreene ws.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***********************@authen.white.readfreene ws.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***********************@authen.white.readfreene ws.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**********************@g43g2000cwa.googlegroups .com> Aleksey
Loginov wrote:

Alexander Stippler wrote:
In <43***********************@authen.white.readfreene ws.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

Alexander Stippler wrote:
In <11**********************@g43g2000cwa.googlegroups .com> Aleksey
Loginov wrote:

Alexander Stippler wrote:
In <43***********************@authen.white.readfreene ws.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.


Especially for "large scale matrix-vector-products" they are good
choice.
For tiny matrices you can implement auto_ptr-like class.
And they are not flexible enough for our purposes.


Expression templates very flexible.

Oct 31 '05 #11
Alexander Stippler wrote:
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.

And what does "A*x" return, if not a temporary?
The second approach does an assignment and two
updates as separated operations. You need to do assignment and two updates anyway. The only question is whether you assign
the whole matrix, and do two updates, or you do assignment and two updates for each
element in the matrix (I understand that is what you do, and indeed this would be a bit
faster because of the CPU cache).
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).


So, in some cases there will be temporaries, and you can not guarantee that the compiler
will optimise them away.
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.

So, you do return a small temporary proxy from all the operators, and then you overload
the assignment operator for your Vector to accept that proxy and do all the calculations
at that point. This is quite a standard technique, but it gets ugly with complicated
expressions, and it also has its limits. In other words, you are delaying the execution of
your expression until you actually need the final result.

In any case, you do not prevent any copying and temporaries, you just have lightweight
proxy temporaries, if I understand your library design.
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.

Then why are you relying on compiler optimisations which you can not guarantee? I did some
work with large scale matrices, and it was much more important for me to guarantee the
performance, than the syntactic sugar. Also, my code was used on many platforms/compilers,
some of which were pretty old, without any performance problems.

--

Valentin Samko - http://www.valentinsamko.com
Oct 31 '05 #12

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

Similar topics

34
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...
5
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...
4
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,...
16
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
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
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...
3
by: Michael | last post by:
Hi All, why does this: class Base { protected: int var; public: Base():var(0){}; virtual int getVar() = 0;
1
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...
12
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? ...
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...
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.