[ITK-users] itk::LightObject does not expose operator<< in .h header

Taylor Braun-Jones taylor at braun-jones.org
Tue Apr 1 09:15:05 EDT 2014


Indeed, I was not aware of argument-dependent name lookup. Thanks for
sharing!

-- Taylor


On Tue, Apr 1, 2014 at 8:55 AM, Bradley Lowekamp <blowekamp at mail.nih.gov>wrote:

> Hello,
>
> I am looking over your patch now.
>
> I just had a comment on you code here... You have place the operator in
> the std namespace. Modifying the std namespace is not allowed, and can give
> warnings or error on some compilers.
>
> These operators should be in the same namespace as the object. C++ should
> find the correct operator called from any namespace due to
> argument-dependent name lookup[1]. This is an advance C++ concept that many
> people don't know they are using, so I'd thought I'd just share the info.
>
> Brad
>
>
> [1] http://en.wikipedia.org/wiki/Argument-dependent_name_lookup
>
> On Mar 21, 2014, at 2:38 PM, Taylor Braun-Jones <taylor at braun-jones.org>
> wrote:
>
> Hello Insight-users,
>
> I'm trying to print an itk::LightObject-derived class with something like:
>
> MyClass::Pointer fooBar = MyClass::New();
> std::cout << *fooBar;
>
>
> But it doesn't seem to be possible because the declaration of operator<<
> is not exposed in itkLightObject.h (although one is defined in
> itkLightObject.cxx). I know I can use fooBar->Print(std::cout), but this
> doesn't work when I'm trying to print to a logging system that was designed
> to support any object with an overloaded operator<< defined. I am able to
> get things to work by defining the operator myself:
>
> namespace std
> {
> ostream &
> operator<<(ostream & os, const itk::LightObject & o)
> {
>    o.Print(os);
>    return os;
> }
> }
>
>
> But is this the intended usage?
>
> Thanks,
> Taylor
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20140401/88f6bf19/attachment.html>


More information about the Insight-users mailing list