简体   繁体   中英

Returning reference to temporary error when remodeling from T ** to vec<vec<T>>

I encountered an error that happend in my proxy class for operator [] . It was checking if index is in range, and worked fine when I implemented my class template with T** values.

But I felt like change whole implementation to std::vector<std::vector<T>> . Everything is fine, expect said operator[] .

Matrix class operator(s)

//***************************************************************************
template <typename T>
X_Proxy<T> Matrix<T>::operator [](const size_t& j)
{
    if(j >= y)
        ERROR_MSG(Y_OUT_RANGE);
    return X_Proxy<T>(inner[j], x);
}

//***************************************************************************
template <typename T>
const X_Proxy<T> Matrix<T>::operator [](const size_t& j) const
{
    if(j >= y)
        ERROR_MSG(Y_OUT_RANGE);
    return X_Proxy<T>(inner[j], x);
}
//***************************************************************************

Proxy class template definition:

template <typename T>
struct X_Proxy
{
    X_Proxy(std::vector<T> PTR, const size_t X) : x_ptr(PTR), x(X) {}
    T& operator [] (size_t pos);
    const T& operator [] (size_t pos) const;
    std::vector<T>& x_ptr;
    const size_t& x;
};

Proxy class operator(s):

//***************************************************************************
template <typename T>
T& X_Proxy<T>::operator [] (size_t pos)
{
    if (pos > x-1)
        Matrix<T>::ERROR_MSG(Matrix<T>::X_OUT_RANGE);
    return x_ptr[pos];
}
//***************************************************************************
template <typename T>
const T& X_Proxy<T>::operator [] (size_t pos) const
{
    if (pos > x-1)
        Matrix<T>::ERROR_MSG(Matrix<T>::X_OUT_RANGE);
    return x_ptr[pos];  // <--- the error line
}

//***************************************************************************

Matrix error function:

template <typename T>
void Matrix<T>::ERROR_MSG(const int& MSG)
{
    std::cerr << info[MSG] << std::endl;
    exit(MSG);
}

Compilation error:

..\matrix.h:47: error: returning reference to temporary [-Wreturn-local-addr]
         return x_ptr[pos];
                         ^

What could go wrong with our lovely template library?

Your X_Proxy constructor is storing a reference to a temporary:

X_Proxy(std::vector<T> PTR, const size_t X) : x_ptr(PTR), x(X) {}

Here, PTR is a local temporary, and x_ptr is an lvalue reference:

std::vector<T>& x_ptr;

This isn't standard C++, so it shouldn't even compile. But your compiler allows it, leaving you with a dangling reference.

Perhaps you want to store a reference to a valid vector:

X_Proxy(std::vector<T>& PTR, const size_t X) : x_ptr(PTR), x(X) {}
                      ^

This will work as long as the vector referred to by PTR outlives the X_Proxy instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM