Scoped_array with constructors?
Hi, I want to use scoped_ptr with std::vector or scoped_array, but the problem is that the type I want to allocate has a non-trivial constructor. What is the standard way to make this work? My current best guess is to use std::vector with boost::scoped_ptr in the following way: TextureUnitsController::TextureUnitsController () { GLint textureUnitCount = 1; glGetIntegerv(GL_MAX_TEXTURE_UNITS, &textureUnitCount); m_textureUnits.resize(textureUnitCount); for (unsigned i = 0; i < textureUnitCount; i += 1) { m_textureUnits[i].reset(new TextureUnit(GL_TEXTURE0 + i)); } std::cerr << "Found " << textureUnitCount << " texture units..." << std::endl; } But this doesn't work because scoped_ptr also has a non-trivial constructor, so resize fails. Thanks Samuel
Hi Samuel, Assuming m_textureUnits is a std::vector, have you tried to use m_textureUnits.reserve instead of resize ? I think that could solve your issue. Then you might also want to consider to use m_textureUnits.push_back instead of m_textureUnits[i]. Kind regards Rune On Tue, Apr 21, 2009 at 12:05 PM, Space Ship Traveller < space.ship.traveller@gmail.com> wrote:
Hi, I want to use scoped_ptr with std::vector or scoped_array, but the problem is that the type I want to allocate has a non-trivial constructor.
What is the standard way to make this work? My current best guess is to use std::vector with boost::scoped_ptr in the following way:
TextureUnitsController::TextureUnitsController () { GLint textureUnitCount = 1; glGetIntegerv(GL_MAX_TEXTURE_UNITS, &textureUnitCount);
m_textureUnits.resize(textureUnitCount);
for (unsigned i = 0; i < textureUnitCount; i += 1) { m_textureUnits[i].reset(new TextureUnit(GL_TEXTURE0 + i)); }
std::cerr << "Found " << textureUnitCount << " texture units..." << std:: endl; }
But this doesn't work because scoped_ptr also has a non-trivial constructor, so resize fails.
Thanks Samuel
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Samuel,
I don't see any explicit reference to scoped_ptr in your code,
so assuming you are trying to use std::vector
Hi Samuel,
Assuming m_textureUnits is a std::vector, have you tried to use m_textureUnits.reserve instead of resize ? I think that could solve your issue. Then you might also want to consider to use m_textureUnits.push_back instead of m_textureUnits[i].
Kind regards
Rune
On Tue, Apr 21, 2009 at 12:05 PM, Space Ship Traveller < space.ship.traveller@gmail.com> wrote:
Hi, I want to use scoped_ptr with std::vector or scoped_array, but the problem is that the type I want to allocate has a non-trivial constructor.
What is the standard way to make this work? My current best guess is to use std::vector with boost::scoped_ptr in the following way:
TextureUnitsController::TextureUnitsController () { GLint textureUnitCount = 1; glGetIntegerv(GL_MAX_TEXTURE_UNITS, &textureUnitCount);
m_textureUnits.resize(textureUnitCount);
for (unsigned i = 0; i < textureUnitCount; i += 1) { m_textureUnits[i].reset(new TextureUnit(GL_TEXTURE0 + i)); }
std::cerr << "Found " << textureUnitCount << " texture units..." << std:: endl; }
But this doesn't work because scoped_ptr also has a non-trivial constructor, so resize fails.
Thanks Samuel
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Space Ship Traveller wrote:
Hi,
I want to use scoped_ptr with std::vector or scoped_array, but the problem is that the type I want to allocate has a non-trivial constructor.
What is the standard way to make this work? My current best guess is to use std::vector with boost::scoped_ptr in the following way:
Have you looked into Thorsten Ottosen's pointer container library?
On Tue, Apr 21, 2009 at 12:05 PM, Space Ship Traveller < space.ship.traveller@gmail.com> wrote:
Hi, I want to use scoped_ptr with std::vector or scoped_array, but the problem is that the type I want to allocate has a non-trivial constructor.
What is the standard way to make this work? My current best guess is to use std::vector with boost::scoped_ptr in the following way:
TextureUnitsController::TextureUnitsController () { GLint textureUnitCount = 1; glGetIntegerv(GL_MAX_TEXTURE_UNITS, &textureUnitCount);
m_textureUnits.resize(textureUnitCount);
for (unsigned i = 0; i < textureUnitCount; i += 1) { m_textureUnits[i].reset(new TextureUnit(GL_TEXTURE0 + i)); }
std::cerr << "Found " << textureUnitCount << " texture units..." << std:: endl; }
But this doesn't work because scoped_ptr also has a non-trivial constructor, so resize fails.
Thanks Samuel
Hi! Just a question. Why not using std::vector in conjunction with reserve and std::fill_n or std::generate_n algorithm? Why do you need your object instances to be heap allocated and not simply constructed in the vector storage? You might be interested by boost assign library to fill vector on the fly, instead of using std algorithms. Regards, Ovanes
Thanks for all the useful replies. Firstly, yes, I attempted to use scoped_ptr into a std::vector which didn't work out. I've taken your advice and used the vector to manage the storage. This is convenient, but does require a copy construction as far as I can tell.. Kind regards, Samuel
Hi!
Just a question. Why not using std::vector in conjunction with reserve and std::fill_n or std::generate_n algorithm? Why do you need your object instances to be heap allocated and not simply constructed in the vector storage? You might be interested by boost assign library to fill vector on the fly, instead of using std algorithms.
Regards, Ovanes
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
Andrew Holden
-
Nikolai N Fetissov
-
Ovanes Markarian
-
Rune Lund Olesen
-
Space Ship Traveller