Foam::DynamicList<Type>.append( new Type );
Howdy Foamers:
I have a C++ question. I think I know the answer empirically, but I don't know if my conception is correct. I have created a DynamicList of some data type (let's call it "Type"). Its starts off with zero elements and I would like to append new terms in it. Currently I would have to declare a new instance in a dummy variable of this "Type" and append this into the list: Code:
Foam::DynamicList<Type> myList(0); Code:
Foam::DynamicList<Type> myList(0); Thanks in advance, Gerry. |
Unfortunately, what you're looking for isn't possible with the way DynamicList is currently written. You're effectively looking for functionality identical to "emplace_back" with std::vector. My suggestion would be to ask the "powers that be" to put this functionality in. It isn't hard - just requires a new member function with variadic template arguments which uses std::forward to pass constructor arguments to the element you're trying to construct in-place.
The alternative is to use a DynamicList with an std::auto_ptr<Type> template argument (or, equivalently, std::shared_ptr / std::unique_ptr since auto_ptr was deprecated). But unless the cost of you copy constructor is large, you should question whether this entire effort is worth it, and maybe you should simply live with the temporary. |
Dear Sandeep:
Thanks for your clarification. My dummy variable is local, and the function in question is only called at initialization. So there is not a lot of overhead (for now). I can live with an extra line of code. The autoPtr thing sounds like something I should explore for some other things I am considering, and I should in the mean time dig a little deeper into it. Sincerely yours, Gerry. |
At the risk of hijacking my own thread ...
Howdy Foamers: Does anyone know where volScalarField is defined? I see GeometricField, which is similar, but no volScalarField. I am asking because I am trying to create a DynamicList<volScalarField>. Code:
DynamicList<volScalarField> myList(0); Code:
lnInclude/List.C:294:21: error: no matching function for call to ‘Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>::GeometricField()’ Code:
Sandeep, this is where I would like to ask for your opinion. Would there be any benefit of making dummyVolScalarField into an autoPtr. Ultimately it would more efficient if I could just reference dummyVolScalarField.ref() to dummy directly. Would this be possible? Thanks in advance, Gerry. |
All times are GMT -4. The time now is 08:47. |