可变参数模板和 std::array 意外行为
Variadic template and std::array unexpected behaviour
我可以编译,但我在运行以下代码中遇到问题(我将其最小化):
#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
}
输出为:
integers[2]=5
integers[1]=10
integers[0]=15
integers[2]=5
integers[1]=1016039760
integers[0]=22034
很明显,数组的前两个单元格在对象初始化后有垃圾编号。我怀疑我以某种方式破坏了堆栈,但我无法弄清楚 why/where...
构造函数中的语句Selector(args...);
,只是构造了一个临时对象,该对象立即被销毁;它与当前对象无关。
我想你想要 delegating constructor (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
}
我可以编译,但我在运行以下代码中遇到问题(我将其最小化):
#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
}
输出为:
integers[2]=5
integers[1]=10
integers[0]=15
integers[2]=5
integers[1]=1016039760
integers[0]=22034
很明显,数组的前两个单元格在对象初始化后有垃圾编号。我怀疑我以某种方式破坏了堆栈,但我无法弄清楚 why/where...
构造函数中的语句Selector(args...);
,只是构造了一个临时对象,该对象立即被销毁;它与当前对象无关。
我想你想要 delegating constructor (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
}