CustomVector<T const, ...> can't be constructed

Issue #152 wontfix
Hartmut Kaiser created an issue

The following snippet fails compiling while it shouldn't:

CustomVector<T const, ...> cm{(T const*)nullptr, 0, 0};

Is CustomVector<T const, ...> supported?

Comments (3)

  1. Klaus Iglberger

    Hi Hartmut!

    Thanks for creating this issue. You are correct, the given code snippet will not work for a couple of reasons. Admittedly, the situation is a little tricky, but the behavior is correct. Allow me to explain.

    First of all, I should point out that CustomVector provides two constructors:

    template< typename Type  // Data type of the vector
            , bool AF        // Alignment flag (aligned or unaligned)
            , bool PF        // Padding flag (padded or unpadded)
            , bool TF >      // Transpose flag (columnVector or rowVector)
    class CustomVector
    {
       // ...
       explicit CustomVector( Type* ptr, size_t n );             // (1)
       explicit CustomVector( Type* ptr, size_t n, size_t nn );  // (2)
       // ...
    };
    

    Constructor (1) is for unpadded custom vectors (it only expects a pointer to the custom array and its size), constructor (2) is for padded custom vectors (it additionally expects the number of padding elements as the third argument). In contrast to constructor (1), constructor (2) has the additional task to initialize the padding elements.

    There is two possible compilation errors and one possible runtime error that you might encounter. The first compilation error is created when you try to specify the number of padding elements when creating an unpadded CustomVector. In this case constructor (2) (the three argument constructor) is not available:

    using blaze::CustomVector;
    using blaze::unaligned;
    using blaze::unpadded;
    
    // Compilation error: Trying to specify the number of padding elements for an unpadded custom vector
    CustomVector<T const, unaligned, unpadded> cm{(T const*)nullptr, 0, 0};
                                         ^                              ^
                                         |                              |
                                     unpadded               Number of padding elements
    

    The second compilation error is created when creating a padded CustomVector with const qualified element types. Blaze requires the padding elements to be mutable in order to prepare them properly:

    using blaze::CustomVector;
    using blaze::unaligned;
    using blaze::padded;
    
    // Compilation error: Blaze requires the padding elements to be mutable
    CustomVector<T const, unaligned, padded> cm{(T const*)nullptr, 0, 0};
                     ^                  ^
                     |                  |
                   const             padded
    

    The last possible problem is a runtime error. It is not allowed to create a CustomVector by passing nullptr (see also issue #105):

    using blaze::CustomVector;
    using blaze::unaligned;
    using blaze::unpadded;
    
    // Throws an exception: It is not allowed to pass 'nullptr' to a custom vector
    CustomVector<T const, unaligned, unpadded> cm{(T const*)nullptr, 0};
    

    All problems can be avoided by creating an unpadded CustomVector via the default constructor:

    using blaze::CustomVector;
    using blaze::unaligned;
    using blaze::unpadded;
    
    // Proper setup of an unpadded custom vector via the default constructor
    CustomVector<T const, unaligned, unpadded> cm{};
    

    I hope this explains the situation and helps to resolve all possible problems. Again, thanks for creating this issue, this will help us to improve the documentation for both CustomVector and also CustomMatrix.

    Best regards,

    Klaus!

  2. Log in to comment