Menu

pass by value in array.h

vasek
2009-09-30
2012-09-15
  • vasek

    vasek - 2009-09-30

    Hi,

    I am having troubles compiling

    Array< class*> A;

    for a pointer to abstract class using Visual Studio. (All is fine under linux)
    Just VS keeps complaining about the class being abstract...

    The trouble is caused by passing arguments by value in array.h, e.g.

    line 44: template<class T> const Array<T> concat(const
    Array<T> &a, const T e);

    when this is changed to pass by reference:

    template<class T> const Array<T> concat(const Array<T>
    &a, const T &e);

    all is fine.

    Is there any reason to use pass by value? If not, I would prepare a patch and
    send it for review.

    Thanks,

    Vasek

     
  • Adam Piątyszek

    Adam Piątyszek - 2009-10-01

    Hi Vasek,

    Please submit a bug report with a patch against the SVN trunk or Git master
    branch.

    Thanks,

    /Adam

     
  • vasek

    vasek - 2009-10-11

    Hi Adam,

    thanks for reply. However, the compilation problem is resolved by one of
    regular updates of VS. So this is no longer pressing issue.

    What remains is a general question if it is better to pass parameters of
    concat by value or reference. I have a slight preference for reference, but I
    am really not sure.

    The patch is actually quite trivial:

    @@ -41,9 +41,9 @@ namespace itpp

    // Forward declarations

    template<class T> class Array;

    //! Append element \c e to the end of the Array \c a

    -template<class T> const Array<T> concat(const Array<T> &a, const T e);
    +template<class T> const Array<T> concat(const Array<T>
    &a, const T &e);

    //! Append element \c e to the beginning of the Array \c a

    -template<class T> const Array<T> concat(const T e, const Array<T> &a);
    +template<class T> const Array<T> concat(const T &e, const
    Array<T> &a);

    //! Concat Arrays \c a1 and \c a2

    template<class T> const Array<T> concat(const Array<T>
    &a1,

    const Array<T> &a2);

    @@ -142,9 +142,9 @@ public:

    Array<T>& operator=(const char* values);

    //! Append element \c e to the end of the Array \c a

    • friend const Array<T> concat <>(const Array<T> &a1, const T e);
    • friend const Array<T> concat <>(const Array<T> &a1, const T& e);
      //! Concat element \c e to the beginning of the Array \c a

    • friend const Array<T> concat <>(const T e, const Array<T> &a);

    • friend const Array<T> concat <>(const T& e, const Array<T> &a);
      //! Concat Arrays \c a1 and \c a2

    friend const Array<T> concat <>(const Array<T> &a1,
    const Array<T> &a2);

    //! Concat Arrays \c a1, \c a2 and \c a3

    @@ -174,7 +174,7 @@ public:

    //! Set the subarray defined by indicies i1 to i2 to Array<T> a.

    void set_subarray(int i1, int i2, const Array<T> &a);

    //! Set the subarray defined by indicies i1 to i2 the element value t.

    • void set_subarray(int i1, int i2, const T t);
    • void set_subarray(int i1, int i2, const T &t);

    protected:

    //! Allocate storage for an array of length \c n

    @@ -469,7 +469,7 @@ void Array<T>::set_subarray(int i1, int i2, const
    Array<T> &a)

    }

    template<class T>

    -void Array<T>::set_subarray(int i1, int i2, const T t)
    +void Array<T>::set_subarray(int i1, int i2, const T &t)

    {

    if (i1 == -1) i1 = ndata - 1;

    if (i2 == -1) i2 = ndata - 1;

    @@ -483,7 +483,7 @@ void Array<T>::set_subarray(int i1, int i2, const T
    t)

    }

    template<class T>

    -const Array<T> concat(const Array<T> &a, const T e)
    +const Array<T> concat(const Array<T> &a, const T &e)

    {

    Array<T> temp(a.size() + 1);

    Vasek

     
  • Adam Piątyszek

    Adam Piątyszek - 2009-10-12

    Hi Vasek,

    I have committed your changes to IT++ repositories. Both the stable itpp-4-0
    branch and trunk include this patch.

    Thanks,

    /Adam

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.