[vtkusers] [vtk-developers] Allowable C++11 Features in VTK

David Gobbi david.gobbi at gmail.com
Mon Mar 27 17:22:29 EDT 2017


The fixed-type enums in C++11 could also be beneficial.  Consider the
following vtkLookupTable code, which declares a few constants in the header
(shown below) and then provides the definition in the .cxx file (not shown):

class vtkLookupTable
{
public:
  static const vtkIdType BELOW_RANGE_COLOR_INDEX;
  static const vtkIdType ABOVE_RANGE_COLOR_INDEX;
  static const vtkIdType NAN_COLOR_INDEX;
  static const vtkIdType NUMBER_OF_SPECIAL_COLORS;
...
};

In C++11 these "static const" members can be defined more efficiently as a
fixed-type enum:

public:
  enum : vtkIdType {
    BELOW_RANGE_COLOR_INDEX = 0,
    ABOVE_RANGE_COLOR_INDEX = 1,
    NAN_COLOR_INDEX = 2,
    NUMBER_OF_SPECIAL_COLORS
  };

Of course VTK already uses enums for most constants (except for the ones
that are still #define'd), so the benefit is for when you need a guarantee
that the constant will be evaluated as a specific integral type.

 - David



On Mon, Mar 27, 2017 at 2:14 PM, Robert Maynard <robert.maynard at kitware.com>
wrote:

> Hi,
>
> Thanks for the feedback.
>
> 1. I agree that when doing template metaprogramming, the alias keyword is
> significantly better than typedef. I will update the document to state that
> we prefer alias over typedef
>
> 2. I am going to update the std::array section to clarify that is
> preferred over raw fixed size 'C' arrays.
>
> 3. This is good information to have. I will add a comment to the scoped
> enums section while I wait for more feedback
>
>
> On Mon, Mar 27, 2017 at 3:30 PM, B B <baljci at hotmail.com> wrote:
>
>> I would suggest the following:
>>
>>
>> - Prefer the use of alias declarations instead of typedef: they do the
>> same but alias declarations are better because they can be templetized (see
>> Scott Meyers, Effective Modern C++, 63-67)
>>
>> - Replace raw arrays with std::array when possible: they are
>> copyable, easier to manipulate and have no extra performance costs compared
>> to raw arrays.
>>
>> - For scoped enums, I would restrict their use to global enums,
>> especially in case of possible name conflicts. For nested enums, I would
>> suggest to maintain the use of unscoped enums for two reasons: first, you
>> don't need to write MyEnum::MyEnumValue each time you use them inside
>> the class implementation; second, from my own experience, their implicit
>> conversion to int can be useful in many cases.
>>
>> Boris
>>
>> ------------------------------
>> *De :* vtkusers <vtkusers-bounces at vtk.org> de la part de Robert Maynard <
>> robert.maynard at kitware.com>
>> *Envoyé :* lundi 27 mars 2017 20:03
>> *À :* VTK Developers; vtk vtk
>> *Objet :* [vtkusers] Allowable C++11 Features in VTK
>>
>> As everyone is aware over the past couple of months we have updated
>> VTK to require a C++11 compiler, but have not explicitly stated what
>> C++11 features are usable.
>>
>> We do not intend to incorporate all features of the language at this
>> time because of incompatibilities with the structure of VTK and/or
>> incomplete support for the features by all of the compilers that VTK
>> aims to support.
>>
>> The current proposed C++11 features, and where they are allowed can be
>> found at:
>>
>> https://docs.google.com/document/d/1h7wIq25d-qimQO8N9sE43fHX
>> KKlHM2sW2ErohfHiuCg/edit?usp=sharing
>>
>> Over the next two weeks please provide feedback, either by commenting
>> on the google document, or replying on the mailing list. Once the two
>> weeks are over, we will integrate the result into the existing coding
>> documentation, and then allow C++11 to be used.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170327/d0dbbc59/attachment.html>


More information about the vtkusers mailing list