# Opinions on dropping support for C++03

**URL:** <https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481>\
**Category:** Community\
**Created:** [December 4, 2017, 9:38pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481 "2017-12-04T21:38:17Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [March 26, 2018, 7:36pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/21 "2018-03-26T19:36:01Z")

</div>

That makes sense, thanks.

What about the possibility of `New()` forwarding to constructors like `std::make_shared()`?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [March 26, 2018, 7:43pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/22 "2018-03-26T19:43:53Z")

</div>

`New` first checks whether any of the registered factories provides a constructor for the class, and if not uses the constructor directly. [Here](https://github.com/InsightSoftwareConsortium/ITK/blob/267f9aa7e9b10430ba0cc49c30b31a63f2457c6b/Modules/Core/Common/include/itkMacro.h#L271-L281) is its implementation.

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [April 10, 2018, 10:24am UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/23 "2018-04-10T10:24:23Z")

</div>

Hi @dzenanz, I don’t think I was clear about what I meant. I’ve done some reading so I can be clearer. Ultimately, I would like to be able to write code like:

`auto my_filter = itk::AFilterType::New(argument1, argument2);`

instead of:

```auto
auto my_filter = itk::AFilterType::New();
my_filter->SetArgument1(argument1);
my_filter->SetArgument2(argument2);

```

I personally find the 1st version clearer and more idiomatic C++ (as it follows the RAII style). I don’t for a minute propose getting rid of the 2nd version though, only adding this as an option.

You can do this with `std::make_shared`, e.g.:

`auto a_pointer = std::make_shared<A_Type>(argument1, argument2);`

This is accomplished by `make_shared` taking a parameter pack as one of its template arguments, and then forwarding that to the constructor. I think it would be relatively straightforward to write a macro that could do this for ITK in the non-factory case. How this would work with factories, I don’t know (perhaps it wouldn’t need to?).

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [April 10, 2018, 1:45pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/24 "2018-04-10T13:45:33Z")

</div>

> [@spinicist](#):
>
> auto my\_filter = itk::AFilterType::New(argument1, argument2);

I think this can be accomplished, and easily too. Add a static method `New` with two arguments, and in implementation do:

```
auto my_filter = itk::AFilterType::New();
my_filter->SetArgument1(argument1);
my_filter->SetArgument2(argument2);
return myFilter;

```

A constructor with two parameters approach might be similarly possible too.

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [April 10, 2018, 1:52pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/25 "2018-04-10T13:52:56Z")

</div>

> [@spinicist](#):
>
> auto my\_filter = itk::AFilterType::New(argument1, argument2);

The ITK Python wrapping [already supports this behavior](https://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html). If possible to have similar behavior, it would be nice to have the same API.

---

<div class="post-metadata">

**Author:** ![seanm](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@seanm](https://discourse.itk.org/u/seanm)\
**Post date:** [April 10, 2018, 2:01pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/26 "2018-04-10T14:01:30Z")

</div>

Your example implies that “SetArgument1” and “argument1” are similarity named and thus obviously redundant. I think a better example would be:

auto my\_filter = itk::AFilterType::New();  
my\_filter-\>SetArgument1(variableA);  
my\_filter-\>SetArgument2(variableB);

vs

auto my\_filter = itk::AFilterType::New(variableA, variableB);

Here’s its more clear that the self-documenting “SetArgument1” is lost.

If C++ had named parameters, like Obj-C or Swift, then you could do something like:

auto my\_filter = itk::AFilterType::New(argument1 : variableA, argument2 : variableB);

But without named parameters, a constructor that just takes a bunch of arguments, especially if they themselves are declared ‘auto’, I think could really hurt readability of the code.

Cheers,

---

<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:** [April 10, 2018, 2:06pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/27 "2018-04-10T14:06:51Z")

</div>

The ITK Python works nicely because it utilizes named arguments which clearly states which argument is which.

In SimpleITK, we have procedural methods and `Execute` methods which takes a list of arguments. It a tricky to maintain the API and keep the order of the arguments. Additionally, there are permutations when an argument can have multiple types. For example, scalar and array. I don’t think this can be properly maintained in ITK manually.

We are able to do it in SimpleITK because the SimpleITK interface is automatically generated from a JSON file which specifies each IVAR/parameter once. Then the IVARs Set/Get methods, procedural interface, and execute interface are all generated consistently. Even options for multiple types of arguments are taken into consideration and the permutations are automatically generated. This can not be done manually.

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [April 10, 2018, 2:17pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/28 "2018-04-10T14:17:11Z")

</div>

@seanm Yes - this was exactly the discussion I thought would occur. It’s a matter of personal preference which to go for. I would prefer the RAII type initialisation because I often forget to add all the required `->SetArgument()` calls and then end up with objects in invalid states which only gets picked up at run-time. RAII is more likely (but not foolproof) to catch problems at compile-time. And yes, named arguments could be the best of both-worlds (although Object-C ends up horribly verbose in my opinion).

@matt.mccormick / @dzenanz I think the best way to implement this would be an additional macro, e.g. `itkForwardNewMacro` that declares the necessary template function? I will try to come up with a patch to show what I have in mind (but my todo list is very long ☹ )

---

<div class="post-metadata">

**Author:** ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)\
**Post date:** [April 11, 2018, 4:01pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/29 "2018-04-11T16:01:53Z")

</div>

> I often forget to add all the required -\>SetArgument() calls and then end up with objects in invalid states

If I’m not mistaken some compilers are able to warn about uninitialized variables, and dynamic analysis tools also report them.

In any case, what the `Coding Style Guide` chapter in Appendix 2 of the [ITK Software Guide](https://itk.org/ItkSoftwareGuide.pdf) says (in section `B.10`):

> All member variables must be initialized in the class constructor. For such purpose, initialization lists are preferred

True that you will find some cases where this has been overlooked. But we’d need to try to fix them whenever we find the,

---

<div class="post-metadata">

**Author:** ![spinicist](https://discourse.itk.org/user_avatar/discourse.itk.org/spinicist/32/183_2.png) [@spinicist](https://discourse.itk.org/u/spinicist)\
**Post date:** [October 1, 2018, 2:21pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/30 "2018-10-01T14:21:22Z")

</div>

Hello,

Apologies for taking 6 months to get round to this, but the following macro does what I want:

```auto
#define itkForwardNewMacro(x) \
  template<class... Args> \
  static Pointer New(Args &&... args) \
    { \
    Pointer smartPtr = ::itk::ObjectFactory< x >::Create(); \
    if ( smartPtr == nullptr ) \
      { \
      smartPtr = new x(std::forward<Args>(args)...); \
      } \
    smartPtr->UnRegister(); \
    return smartPtr; \
    }

```

@jhlegarreta There’s different levels of uninitialized. For instance, if I forget to set the second input to an `AddImageFilter`, ITK will not crash, but it will correctly throw an exception telling me that the input hasn’t been set. I’d prefer to pick that up at compile time by not having the correct number of arguments to my constructor, which the current architecture prevents. The closer I can get to RAII the better, but I am aware I may be in the minority with that opinion.

---

<div class="post-metadata">

**Author:** ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)\
**Post date:** [October 2, 2018, 2:02am UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/31 "2018-10-02T02:02:59Z")

</div>

Glad to see that you found the macro useful.

I guess it was a design option to disallow/not use such constructors. May be some of the more experienced folks can elaborate more on the reasons.

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [October 2, 2018, 6:50pm UTC](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481/32 "2018-10-02T18:50:39Z")

</div>

For the example of `AddImageFilter` and similar existing implementations, if we want to pass the inputs in the construction of the object via `New(Args ... args)`, the number of required inputs in existing implementations is defined in constructors, with `SetNumberOfRequiredInputs`, so the implementation could like something like (untested):

```auto
if ( smartPtr == nullptr )
  {
  unsigned int index = 0;
  smartPtr = new x();
  for ( auto input : {args...} )
    {
    smartPtr->SetInput( index, input );
    ++index
    }

```

[Previous page](https://discourse.itk.org/t/opinions-on-dropping-support-for-c-03/481.md?page=1)
