1
요소 유형에 기본 생성자가없는 경우 모든 중복 요소가 포함 된 템플릿 배열을 구성하는 방법이 있습니까?기본 생성자가없는 중복 요소의 템플릿 배열
template<typename T, int n> struct Array {
template<typename... Args> explicit Array(const T& arg1, Args... args)
: m_a{arg1, args...} { }
static Array<T,n> all(const T& value) {
Array<T,n> ar; // error: use of deleted function 'Array<TypeWithoutDefault, 10>::Array()'
for (int i=0; i<n; i++)
ar.m_a[i] = value;
return ar;
}
T m_a[n];
};
struct TypeWithoutDefault {
TypeWithoutDefault(int i) : m_i(i) { }
int m_i;
};
int main() {
// works fine
Array<TypeWithoutDefault,2> ar1 { TypeWithoutDefault{1}, TypeWithoutDefault{2} };
(void)ar1;
// I want to construct an Array of n elements all with the same value.
// However, the elements do not have a default constructor.
Array<TypeWithoutDefault,10> ar2 = Array<TypeWithoutDefault, 10>::all(TypeWithoutDefault(1));
(void)ar2;
return 0;
}
좋아하면'repeat' 기능이 필요
나는 다음을 시도했다. 내 대답 좀 봐 : [T는 기본 constructible 경우 std :: array그게 효과가 있는지 알려주세요. 그렇지 않은 경우 수정하고 답변으로 게시합니다. – Nawaz
감사합니다. 제 질문은 불행히도 여러 사람이 지적한 중복 질문이었습니다. @ Nawaz와 @ Jarod42의 대답은 모두 매우 적절합니다. 'index_sequence'와'make_index_sequence'의 간결한 구조는 매우 좋습니다. – Hugues