Search code examples
c++templatessizeof

C++ Template Parameter sizeof Returns Incorrect Result


That way I can find the number of elements in an array. However, when I put this int array as a template as a parameter, the result is not calculated correctly

int arr[] = {1,2,3,4,5};
int size_arr  =  sizeof(arr) / sizeof(arr[0]);

I add an INT Array to the List as a parameter with the type template.

template <typename listType, typename arrayX>
 listType  addList(listType e , arrayX array)
{
     int        sizeOf = sizeof(array);
     int        sizeOfperOne = sizeof(array[0]);
     int        arrSize =  sizeOf   /   sizeOfperOne        ;
     cout << "Total Byte :  " << sizeOf << "     BytePerUnit : " << sizeOfperOne << " arrSize : " << arrSize<< endl;
     for (int i = 0; i <  arrSize; i++)
    {
        e.push_back(array[i]);
    }
    return e;
}

And the other Template and Method in created to print this List content

template    <typename T>
void print(T& t, string name)
{
    typename T::iterator i = t.begin();
    cout << name << "\tMembers  ==>>>   ";
    while (i != t.end())
    {
        if (i == t.begin())
        {
            cout << *i++;
        }
        else
        {
            cout << " - " << *i++;
        }
    }
    cout << endl;
}

int main() 
{
    int mlArray[] = { 1,2,3,4,5};
    
    list<int>   MasterListe ;
    MasterListe = addList(MasterListe, mlArray);
    cout << "MasterListe SizeOf :    " << MasterListe.size() << endl;
    print(MasterListe, "MasterList      : ");
    return 0;
}

Total Byte : 8 BytePerUnit : 4 arrSize : 2

MasterListe SizeOf : 2
MasterList : Members ==>>> 1 - 2

Array is filled with numbers 1,2,3,4,5, although 5 units are passed, the return value is 1 and 2.

I may also want to create the list that I am currently using in the INT type from the class below.

 list<TradeList> 

class TradeList
{
    public:
            int      PosTicket  ;
            strinh   Pairs      ;
            double   OpenPrice  ;
            double   StopLoss   ;
            double   TakeProfit ;
}

Believe me, I couldn't find a solution to this through my research.

Thank you so much for your help.


Solution

  • The main issue is that arrays decay to pointers, thus the values of sizeof() in your template function addList actually is attempting to get sizeof(int *).

    If all that addList does is add items to the std::list, there are generic ways to do this without need to create another function.

    One way is to use std::copy_n:

    #include <iostream>
    #include <list>
    #include <algorithm>
    #include <iterator>
    #include <string>
    
    class TradeList
    {
        public:
            int      PosTicket  ;
            std::string   Pairs      ;
            double   OpenPrice  ;
            double   StopLoss   ;
            double   TakeProfit ;
    };
    
    int main()
    {
        TradeList mlArray[5];
        std::list<TradeList>   MasterListe;
        std::copy_n(mlArray, std::size(mlArray), std::inserter(MasterListe, MasterListe.end()));
        std::cout << MasterListe.size();
    }
    

    Output:

    5