ocirocir
ocirocir

Reputation: 4058

Variadic template and std::array unexpected behaviour

I can compile but I have problems in running the following code (I minimized it):

#include <iostream>
#include <array>


template<int N>
class Selector {

public:

    template <typename... Args>
    Selector(int x, Args... args) noexcept {
        integers[sizeof...(Args)] = x;
        std::cout << "integers[" << sizeof...(Args) << "]=" << integers[sizeof...(Args)] << std::endl;  // OK

        Selector(args...);
    }

    Selector(int x) noexcept {
        integers[0] = x;
        std::cout << "integers[0]=" << integers[0] << std::endl;    // OK
    }


    void print_num(int i) const {
        std::cout << "integers[" << i << "]=" << integers[i] << std::endl;
    }

private:
    std::array<int, N> integers;
};

int main() {
    Selector<3> x(5, 10, 15);

    x.print_num(2); // OK
    x.print_num(1); // KO
    x.print_num(0); // KO

}

The output is:

integers[2]=5
integers[1]=10
integers[0]=15
integers[2]=5
integers[1]=1016039760
integers[0]=22034

Clearly the first two cells of the array have garbage numbers after the object initialization. I suspect that I'm corrupting the stack in some way, but I cannot figure out why/where...

Upvotes: 2

Views: 46

Answers (1)

songyuanyao
songyuanyao

Reputation: 172934

The statement Selector(args...); in the constructor, just constructs a temporary object which gets destroyed immediately; it does nothing relevent to the current object.

I suppose you want delegating constructor (since C++11):

template <typename... Args>
Selector(int x, Args... args) noexcept : Selector(args...) {
//                                     ^^^^^^^^^^^^^^^^^^^
    integers[sizeof...(Args)] = x;
    std::cout << "integers[" << sizeof...(Args) << "]=" << integers[sizeof...(Args)] << std::endl;  // OK
}

LIVE

Upvotes: 4

Related Questions