<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<div class="moz-text-html" lang="x-western"> Frederic Perez wrote:
<blockquote
cite="mid:AANLkTimVuUdWS1ROpsF_0ye8UjE6gpcejg5cO4ATTz-N@mail.gmail.com"
type="cite">Hello Stuart,<br>
<br>
I would like to be able to do those kinds of assignments too!<br>
<br>
For example, in our group we have functions like this one:<br>
<br>
<span style="font-family: courier new,monospace;">
template<typename TITKImgType></span><br
style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> bool</span><br
style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> WriteFile(</span><br
style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> const
std::string& a_fullFilename,</span><br
style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> typename
TITKImgType::<span style="background-color: rgb(255, 255, 153);">ConstPointer</span>);</span><br>
<br>
declared this way to promise that <span
style="font-family: courier new,monospace;">WriteFile </span>won't
modify the input image.<br>
(By the way, the template stuff is not relevant to this thread.)<br>
<br>
In order to workaround the const-vs-nonconst problem, we do calls like
this:<br>
<br>
<div style="margin-left: 40px; font-family: courier new,monospace;">typedef
itk::Image<int, 3> ImgType;<br>
<span style="background-color: rgb(255, 255, 255);">ImgType::<span
style="background-color: rgb(255, 255, 153);">Pointer </span></span>image
= ImgType::New();<br>
...<br>
</div>
<div style="margin-left: 40px; font-family: courier new,monospace;">WriteFile<ImgType>("out.mhd",
image<span style="background-color: rgb(255, 255, 153);">.GetPointer()</span>);<br>
</div>
<br>
Unfortunately, this populates the code with lots of <span
style="font-family: courier new,monospace;">GetPointer()</span> that
we would like to get rid of.<br>
Not a real solution, but maybe you can use it.<br>
</blockquote>
Seems better than my "solution" at least, cheers :) The other
alternative I'd been contemplating was to write a wrapper that can be
(implicitly) constructed from either a Pointer or a ConstPointer. You
then use parameters of the wrapper type (or const& to the wrapper),
and unwrap them within the function. That might make the called code
marginally longer, but cleans up the code at the call sites.<br>
<br>
I guess if you wrote a full wrapper with a conversion operator back to
itk::SmartPointer then you might be able to make the whole thing
relatively seamless. It would be a lot better if they just changed
itk::SmartPointer to allow this, though - unless there's some
compelling reason not to that I've missed of course. I wonder how much
bureaucracy would be required for anyone to even contemplate that sort
of change though(!)<br>
<br>
Cheers,<br>
Stu<br>
<blockquote
cite="mid:AANLkTimVuUdWS1ROpsF_0ye8UjE6gpcejg5cO4ATTz-N@mail.gmail.com"
type="cite">I would like to hear more on this issue.<br>
<br>
Cheers,<br>
<br>
Frederic Perez<br>
<br>
<div class="gmail_quote">On Thu, May 27, 2010 at 7:21 PM, Stuart
Golodetz <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:itk@gxstudios.net">itk@gxstudios.net</a>></span> wrote:<br>
<blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000">
<div style="font-family: -moz-fixed; font-size: 13px;"
lang="x-western">
<pre>Hi,
Hope this is the right place to post this. I was just wondering if
there's a reason why itk::SmartPointer was designed so as not to allow e.g.
itk::Image<int,3>::Pointer image;
const itk::Image<int,3>::ConstPointer& cimage = image;
?
The equivalent with boost::shared_ptr is allowed, e.g.
boost::shared_ptr<int> p(new int);
const boost::shared_ptr<const int>& cp = p;
This doesn't seem like a major problem, until you start writing
functions taking const itk::Image<...>::ConstPointer& parameters - at
which point it won't let you pass a normal Pointer in without explicitly
constructing a ConstPointer from it. Now the types are often quite long,
and it's annoying to have to add extra typedefs in the calling code just
for that purpose. Duplicating the functions with const
itk::Image<...>::Pointer& parameters doesn't work either, because you
get a combinatorial explosion when you have multiple such parameters.
For instance, with 3 parameters, you have to create functions with
combinations:
const Pointer&, const Pointer&, const Pointer&
const Pointer&, const Pointer&, const constPointer&
const Pointer&, const ConstPointer& const Pointer&
// more here
const ConstPointer&, const ConstPointer& const ConstPointer&
This seems like an unproductive way to spend one's time, to say the
least. The only other "reasonable" alternative I've managed to come up
with that doesn't either (a) clutter up the call site or (b) cause the
combinatorial explosion just outlined, is to just use the non-const
Pointers everywhere and abandon the idea of making the code
const-correct. But that seems defeatist to me <span><span> :) </span></span> Please could you tell
me if there's something I'm missing? (And if so, what?)
Cheers,
Stuart
</pre>
</div>
</div>
<br>
_____________________________________<br>
Powered by <a moz-do-not-send="true" href="http://www.kitware.com"
target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a moz-do-not-send="true"
href="http://www.kitware.com/opensource/opensource.html"
target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a moz-do-not-send="true"
href="http://www.kitware.com/products/protraining.html" target="_blank">http://www.kitware.com/products/protraining.html</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a moz-do-not-send="true" href="http://www.itk.org/Wiki/ITK_FAQ"
target="_blank">http://www.itk.org/Wiki/ITK_FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a moz-do-not-send="true"
href="http://www.itk.org/mailman/listinfo/insight-users"
target="_blank">http://www.itk.org/mailman/listinfo/insight-users</a><br>
</blockquote>
</div>
</blockquote>
</div>
</body>
</html>