C++ Template Parameter sizeof Returns Incorrect Result

QuanTrader

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.

PaulMcKenzie

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

<sizeof(T)> as inherited class template parameter in c++

Incorrect sizeof() of template argument when inheriting from unordered_map in visual c++

MySQL MAX returns incorrect result

incorrect deduction of template parameter pack

Why does sizeof operator on an array give incorrect result?

File.isFile() returns incorrect result?

Gremlin using select in hasId returns incorrect result

lambdified sympy expression returns incorrect result

getting an array from webservice returns an incorrect result

removing rows without returns incorrect result

Kadane algorithm implementation returns incorrect result

PL/pgSQL function returns incorrect bitwise result

Azure pipeline powershell script returns incorrect result

TimeZoneInfo.ConvertTimeFromUtc returns incorrect result

MarkLogic Query By Example returns incorrect result

Array formula with SUM Function returns incorrect result

Google Maps Autocomplete Place returns incorrect result

in MySQL, MAX() command returns an incorrect result

Descendants() with a parameter returns empty result

C++ 2d array using vectors returns a consistent incorrect result on the second query

Result of sizeof for C++ arrays and pointers

C++ Template Parameter that evaluates a Template (template template parameter)

result_of of call to member function of template parameter

wcslen() returns incorrect result when pragma pack used

Count of multiple items in one MySQL query returns an incorrect result

Fetch DPI from JPEG without library returns incorrect result

numpy.square returns incorrect result for sparse matrices

GROUP BY on column alias with NULLs returns incorrect result in MySQL

Eloquent ORM 5.1 query returns incorrect result (compared to plain SQL)