QuanTrader
QuanTrader

Reputation: 13

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.

Upvotes: 1

Views: 295

Answers (3)

pvc
pvc

Reputation: 1180

Array decays to pointers, if you need the size of array inside template function, you can change your function to below.

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

Upvotes: 2

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

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

Upvotes: 2

Umit Terzi
Umit Terzi

Reputation: 106

I don't think it's a waste of time. Handle the extra operations in the Main method.

 template <typename listType, typename arrayX>
 listType  InsertList(listType e, arrayX array, int size)
 {

     for (int i = 0; i < size; i++)
     {
         e.push_back(array[i]);
     }
     return e;
 }

Main Method

int main() 
{
    int mlArray[] = { 1,2,3,4,5,6,7,8,9};

    list<int> SecondList;
 
    ThirdList=  InsertList(SecondList, mlArray, size(mlArray));
    print(SecondList, "SecondList : ");

    return 0;
}

Upvotes: 0

Related Questions