1
class myclass{
//definitions here
};
myclass e;
int myarray[10];
/*
Do something...
*/
e = myarray;

In order to make e = myarray possible, I overloaded the = operator. And I have to get the length of the incoming array length.

template <class T>
int getarrlen(T& arr)
{
    return sizeof(arr) / sizeof(arr[0]);
}

myclass::operator=(int obj[]) {
    int len=getarrlen(obj);
    //Do something...
}

But the return value of getarrlen(obj) is always 1.

So, how can I get the length of obj[] in the overloading function? By the way, I've also tried int size = *(&arr + 1) - arr; and it didn't work either.

Update0: For this:

template<typename T1, int size>
int getarrlen(T1(&)[size]) { return size; }

I got a C2784 compiler-error in Visual Studio... Strange... Update1: The code provided by the link from @AlgirdasPreidžius works for the main function, but it doesn't work for my code:( Also, to make it more obvious, I've tried this:

#include<iostream>
using namespace std;
int x[10];
//Begin of the copied code
template <std::size_t N>
struct type_of_size
{
    typedef char type[N];
};

template <typename T, std::size_t Size>
typename type_of_size<Size>::type& sizeof_array_helper(T(&)[Size]);

#define sizeof_array(pArray) sizeof(sizeof_array_helper(pArray))
//End
void myv(int a[]) {
    const std::size_t n = sizeof_array(a); // constant-expression!
    cout << n << endl;
}

int main() {
    int a[20] = {1,2,3,4,5};
    myv(a);
}

The code doesn't work. And I've tried to add template <typename T, std::size_t Size> above void myv(int a[]) { and it didn't work work either...

deoplljj
  • 11
  • 2
  • 4
    Your template for deducing the array length is wrong. Have a look at this question: [How does this “size of array” template function work?](https://stackoverflow.com/questions/3368883/how-does-this-size-of-array-template-function-work) – Algirdas Preidžius May 13 '20 at 16:04
  • That won't work, you need to get the array *size* as a template argument as well. How to do it is shown and easily found all over the Internet. – Some programmer dude May 13 '20 at 16:05
  • `myclass::operator=(std::pair ptr_and_len)` – Eljay May 13 '20 at 16:17
  • With the answer in the link provided by @AlgirdasPreidžius MSCV doesn't seem to have a problem: [Demo on CompilerExplorer](https://gcc.godbolt.org/z/BnBKxx) – Scheff's Cat May 13 '20 at 16:31
  • 1
    use the same template overload for `operator=` as you did for sizeof (after fixing the latter...) – M.M May 14 '20 at 01:06
  • @Someprogrammerdude: Maybe I am mistaken in my answer below, but I do not need the size as template parameter. The compiler knows it and can deduce it automatically for us. Right? – A M May 16 '20 at 06:41

1 Answers1

0

I prepared a solution that allows you to do assignments as shown on the top of your questions.

I implemented an assignment operator and a copy constructor with the same mechanism. I will work only for C-Style int arrays at the moment, because the std::vector<int> the class contains ints.

We need to use templates so that the Compiler has a chance to deduce types.

The software has bee tested with Microsoft Visual Studio Community 2019 Version 16.5.2.

Please see:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

struct Test {
    // The data. At the moment fixed to "int"
    std::vector<int> data{};

    // Default constructor
    Test() {}

    // Copy constructor
    template<typename T>
    Test(T& t) {

        // Number of Elements
        size_t size = (sizeof (decltype(t))) / (sizeof (std::remove_extent<std::remove_reference<decltype(t)>::type>::type));
        // Copy the data
        data.clear();
        std::copy_n(&t[0], size, std::back_inserter(data));
    }

    // Assignment operator
    template<typename T>
    Test &operator =(T& t) {

        // Number of Elements
        size_t size = (sizeof(decltype(t))) / (sizeof(std::remove_extent<std::remove_reference<decltype(t)>::type>::type));
        // Copy the data
        data.clear();
        std::copy_n(&t[0], size, std::back_inserter(data));
        return *this;
    }
};

int main() {

    // Some simple Lambda to print the contents of an STL container
    auto print = [](const auto& container) -> void { std::copy(container.begin(), container.end(),
        std::ostream_iterator<std::decay<decltype(*container.begin())>::type>(std::cout, " ")); std::cout << '\n'; };

    // Define an Plain C-Style array
    int a[10] = {0,1,2,3,4,5,6,7,8,9};

    // Define instance of class
    Test t1;
    // Assignment
    t1 = a;

    print(t1.data);

    // Define instance of another class and use copy constructor
    Test t2(a);

    print(t2.data);
    return 0;
}
A M
  • 14,694
  • 5
  • 19
  • 44
  • I don’t recommend overloading `operator =` for an arbitrary type. Instead you should probably overload it for reference-to-array only. – Konrad Rudolph May 15 '20 at 12:21
  • I would never do that, but it is answering the question of the OP. You should comment on the question of the OP instead . . . – A M May 15 '20 at 12:25
  • My comment isn’t about OP’s question (which is reasonable, though gets confused about the difference between arrays and pointers). – Konrad Rudolph May 15 '20 at 12:27