# Initializing itk::vector or numeric type with the same command

**URL:** https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984
**Category:** Engineering
**Created:** [June 5, 2018, 3:28pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984 "2018-06-05T15:28:23Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cyril.mory](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/71c47a/32.png) [@cyril.mory](https://discourse.itk.org/u/cyril.mory)
#### Post date: [June 5, 2018, 3:28pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/1 "2018-06-05T15:28:24Z")

</div>

Hello,

I would like one of my filters to work with ImageType = itk::Image\<float, 3\> as well as with ImageType = itk::Image\<itk::Vector\<float, 5\>, 3\>. Computation on the pixels only involves operators that are overloaded for itk::Vector, like +, -, multiplication by a scalar, etc… so that part is not a problem. What is a problem, though, is the initialization of the image’s pixels to 0. I’d like to have a command to set a pixel to either 0, or to an itk::Vector filled with zeros. At first, I thought that itk::NumericTraits::Zero would be fine for both cases, but my compiler throws warnings for the vector part.

Is there a way to do what I need ? Would it be possible, for example, to add a specialization to itk::NumericTraits to handle the case where the template type is an itk::Vector ?

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [June 5, 2018, 5:04pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/2 "2018-06-05T17:04:14Z")

</div>

Hi Cyril,

> [@cyril.mory](#):
>
> At first, I thought that itk::NumericTraits::Zero would be fine for both cases, but my compiler throws warnings for the vector part.

What kind of warnings do you get? Would you also get these warnings if you would initialize the pixel values by `PixelType{}`, instead of `itk::NumericTraits<PixelType>::Zero`?

Kind regards, Niels

---

<div class="post-metadata">

### Author: ![cyril.mory](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/71c47a/32.png) [@cyril.mory](https://discourse.itk.org/u/cyril.mory)
#### Post date: [June 6, 2018, 12:00pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/3 "2018-06-06T12:00:29Z")

</div>

Hi Niels,

Here is the warning:

In file included from /Users/srit/src/rtk/rtk/include/rtkMechlemOneStepSpectralReconstructionFilter.h:263:  
/Users/srit/src/rtk/rtk/include/rtkMechlemOneStepSpectralReconstructionFilter.hxx:58:90: warning: instantiation of variable ‘itk::NumericTraits\<itk::Vector\<float, 3\> \>::Zero’ required here, but no  
definition is available [-Wundefined-var-template]  
m\_ProjectionsSource-\>SetConstant(itk::NumericTraits::Zero);  
^  
/Users/srit/src/rtk/rtk/include/rtkMechlemOneStepSpectralReconstructionFilter.h:125:17: note: in instantiation of member function  
‘rtk::MechlemOneStepSpectralReconstructionFilter\<itk::Image\<itk::Vector\<float, 3\>, 3\>, itk::Image\<itk::Vector\<float, 5\>, 3\>, itk::Image\<itk::Vector\<float, 150\>, 2\>  
\>::MechlemOneStepSpectralReconstructionFilter’ requested here  
itkNewMacro(Self)  
^  
/Users/srit/src/rtk/rtk/applications/rtkspectralonestep/rtkspectralonestep.cxx:171:66: note: in instantiation of member function  
‘rtk::MechlemOneStepSpectralReconstructionFilter\<itk::Image\<itk::Vector\<float, 3\>, 3\>, itk::Image\<itk::Vector\<float, 5\>, 3\>, itk::Image\<itk::Vector\<float, 150\>, 2\> \>::New’ requested here  
MechlemFilterType::Pointer mechlemOneStep = MechlemFilterType::New();  
^  
/Users/srit/src/itk/itk/Modules/Core/Common/include/itkNumericTraitsVectorPixel.h:222:38: note: forward declaration of template entity is here  
static const Self ITKCommon\_EXPORT Zero;  
^  
/Users/srit/src/rtk/rtk/include/rtkMechlemOneStepSpectralReconstructionFilter.hxx:58:90: note: add an explicit instantiation declaration to suppress this warning if  
‘itk::NumericTraits\<itk::Vector\<float, 3\> \>::Zero’ is explicitly instantiated in another translation unit  
m\_ProjectionsSource-\>SetConstant(itk::NumericTraits::Zero);

Following your suggestion, I tried using PixelType{} instead of itk::NumericTraits::Zero. It compiles without warning when PixelType = itk::Vector\<float, 5\>, but fails to compile when PixelType=float. Also, I did not know about this syntax. Can you direct me to some documentation on it ?

Regards,  
Cyril

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [June 6, 2018, 1:07pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/4 "2018-06-06T13:07:36Z")

</div>

@cyril.mory Now I see your point: it appears that `itk::NumericTraits<T>::Zero` is only instantiated for a limited number of types (`T`): [ITK/Modules/Core/Common/src/itkNumericTraits.cxx at v5.0a02 · Kitware/ITK · GitHub](https://github.com/Kitware/ITK/blob/v5.0a02/Modules/Core/Common/src/itkNumericTraits.cxx)@blowekamp @hjmjohnson Maybe you guys can elaborate on whether or not `itk::NumericTraits<itk::Vector<float, 3> >::Zero` should be supported…?

> [@cyril.mory](#):
>
> Following your suggestion, I tried using PixelType{} instead of itk::NumericTraits::Zero. It compiles without warning when PixelType = itk::Vector\<float, 5\>, but fails to compile when PixelType=float.

Can you tell us the error message you got from `PixelType{}`, when `PixelType` = float? Do you get the same error message when you do `PixelType()` instead?

In general, both Type{} and Type() should yield an initialized object of the specified type, in C++. For a scalar type (like float), it should yield zero. If you want to know more, please search for C++ value-initialization, default-initialization, uniform initialization syntax, braced initialization, aggregate initialization, initializer list, etc.!

---

<div class="post-metadata">

### Author: ![cyril.mory](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/71c47a/32.png) [@cyril.mory](https://discourse.itk.org/u/cyril.mory)
#### Post date: [June 6, 2018, 2:03pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/5 "2018-06-06T14:03:20Z")

</div>

My bad, I tried it again `PixelType{}` with `PixelType` = float and it compiles without problem. I must have done something wrong the first time. Now I’ll just have to replace the `itk::NumericTraits<T>::Zero` with `T{}` everywhere in my code.

Still, I’m interested in Bradley and Hans’ answers on whether or not `itk::NumericTraits<itk::Vector<float, 3> >::Zero` should be supported.

Thanks for the information on braced initialization, too.

---

<div class="post-metadata">

### Author: ![cyril.mory](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/71c47a/32.png) [@cyril.mory](https://discourse.itk.org/u/cyril.mory)
#### Post date: [June 6, 2018, 3:09pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/6 "2018-06-06T15:09:14Z")

</div>

Sorry, that was not the end of it: using `PixelType{}`, my code compiles fine, but returns incorrect results.  
Only the first element of the vector is correctly initialized to zero. So for the time being, I’ll stick to itk::NumericTraits, which throws warnings but seems to do the job.

```
typedef itk::Vector<float, 5> VectorType;
std::cout << VectorType{} << std::endl;
std::cout << VectorType() << std::endl;
std::cout << itk::NumericTraits<VectorType>::Zero << std::endl;

```

returns:

[0, 4.56095e-41, -0.000420635, 4.59149e-41, 0]  
[0, 4.56095e-41, -0.000420635, 4.59149e-41, 0]  
[0, 0, 0, 0, 0]

I’m still using ITK 4.13.0, if that matters.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [June 6, 2018, 3:45pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/7 "2018-06-06T15:45:19Z")

</div>

> [@cyril.mory](#):
>
> Still, I’m interested in Bradley and Hans’ answers on whether or not itk::NumericTraits\<itk::Vector\<float, 3\> \>::Zero should be supported.

That should work.

After looking again, I believe that the `NumericTraits<>::ZeroValue()` constexpr function is preferred over the static variable `Zero`.

There are several ITK array-like classes which are plain old data (POD) and do not perform initialization.

---

<div class="post-metadata">

### Author: ![cyril.mory](https://discourse.itk.org/letter_avatar_proxy/v4/letter/c/71c47a/32.png) [@cyril.mory](https://discourse.itk.org/u/cyril.mory)
#### Post date: [June 12, 2018, 1:15pm UTC](https://discourse.itk.org/t/initializing-itk-vector-or-numeric-type-with-the-same-command/984/8 "2018-06-12T13:15:37Z")

</div>

Indeed, with `NumericTraits<>::ZeroValue()` instead of `NumericTraits<>::Zero`, I get the correct behavior and the warnings disappear.

Thanks !
