473,804 Members | 3,030 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
11 1872

Alexander Stippler wrote:
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.


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
3117
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
3761
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
2651
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
7221
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
9706
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
9579
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
10571
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
9143
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
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
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
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.