简体   繁体   中英

std::array template function

I have a simple function to shuffle elements in an std::array

template <typename T, uint size>
void Shuffle(std::array<T, size> &arr) {
    for (uint i = 0; i < arr.size() - 1; i++)
    {
        std::swap(arr[i], arr[Random(i, arr.size())]);
    }
}

make/g++ doesn't like the way I'm declaring this, giving the error "variable or field Shuffle declared void." From what I've found, this is probably an irrelevant error message, but I can't figure out what's actually wrong here.

uint doesn't match the type of the 2nd template parameter of std::array , which is std::size_t . This causes template argument deduction failing when calling Shuffle , unless you specify template arguments explicitly.

You should declare the 2nd template parameter with type std::size_t . eg

template <typename T, std::size_t size>
void Shuffle(std::array<T, size> &arr)

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