P
Peng Yu
Hi,
It seems that an temporary can not be an argument. Since the last two
statements below does not work. I have to have declare one array and
then initialized an A object with the array, which is cumbersome. I'm
wondering if there is any walkaround, or I have to until the new C++
standard?
Thanks,
Peng
#include <vector>
#include <iostream>
template <typename T>
class A {
public:
template <size_t N>
A(const T(&v)[N]) {
for(size_t i = 0; i < N; ++ i)
_v.push_back(v);
}
private:
std::vector<T> _v;
};
int main() {
double v[] = {0, 1, 2, 3 };
A<double> a1(v);
A<double> a2 = v;
A<double> a3 = {0, 1, 2, 3};//error
A<double> a4({0, 1, 2, 3});//error
}
It seems that an temporary can not be an argument. Since the last two
statements below does not work. I have to have declare one array and
then initialized an A object with the array, which is cumbersome. I'm
wondering if there is any walkaround, or I have to until the new C++
standard?
Thanks,
Peng
#include <vector>
#include <iostream>
template <typename T>
class A {
public:
template <size_t N>
A(const T(&v)[N]) {
for(size_t i = 0; i < N; ++ i)
_v.push_back(v);
}
private:
std::vector<T> _v;
};
int main() {
double v[] = {0, 1, 2, 3 };
A<double> a1(v);
A<double> a2 = v;
A<double> a3 = {0, 1, 2, 3};//error
A<double> a4({0, 1, 2, 3});//error
}