英文:
How to initialize class object with array reference passed as a template argument to constructor in C++ 17 using braced initializator?
问题
template<typename T, size_t ar_sz> class Test_class {
T array[ar_sz];
public:
Test_class(const T (&array)[ar_sz]) {
}
};
int main() {
constexpr size_t array_sz = 1;
int ar_i[array_sz]{ 1 };
Test_class test_ar{ ar_i };
}
要正确传递数组引用到构造函数中,您已经做得很正确了。
英文:
I have a template class and want to pass a reference to an array in its constructor, using braced initializator.
template<typename T, size_t ar_sz> class Test_class {
// Thats incorrect
/*
T t;
size_t size;
*/
// Thats correct?
T array[ar_sz];
public:
Test_class(const T (&array)[ar_sz]) {
}
};
int main() {
constexpr size_t array_sz = 1;
int ar_i[array_sz]{ 1 };
Test_class test_ar{ ar_i };
}
So how to properly do that?
EDIT:
I want to add that i need to pass L"test"
to a constructor, which is a const wchar_t[5]
, so this task cant be resolved using std::array<>
答案1
得分: 2
编译器推断 T = int
和 ar_sz = 1
。在你的模板中,声明 T t;
和 int t;
意义相同。你不想要这样!你想要 T t[ar_sz];
。顺便说一下,为了减少混淆,使用大于 1 的数组大小。
另一个问题是(正如其他人指出的):你不能使用另一个数组来初始化数组。你需要显式复制。
#include <iostream>
#include <algorithm>
template<typename T, size_t size>
struct Test
{
public:
T m_values[size]; // 数组,不是元素(你也可以使用 std::array)
Test(const T (&values)[size])
{
std::copy(values, values + size, m_values); // 显式复制(你也可以使用 memcpy)
}
const auto& values() const noexcept
{
return m_values;
}
};
int main()
{
Test test("meh");
std::cout << test.values()[1] << '\n';
std::cout << test.values()[2] << '\n';
return 0;
}
希望这对你有帮助。
英文:
The compiler deduces T = int
and ar_sz = 1
. In your template, declaration T t;
means the same as int t;
. You don't want this! You want T t[ar_sz];
. BTW to reduce confusion, use array size greater than 1.
Another problem is (as other people noted): you can't initialize an array using another array. You need explicit copying.
#include <iostream>
#include <algorithm>
template<typename T, size_t size>
struct Test
{
public:
T m_values[size]; // array, not element (you could also use std::array)
Test(const T (&values)[size])
{
std::copy(values, values + size, m_values); // explicit copying (you could also use memcpy)
}
const auto& values() const noexcept
{
return m_values;
}
};
int main()
{
Test test("meh");
std::cout << test.values()[1] << '\n';
std::cout << test.values()[2] << '\n';
return 0;
}
答案2
得分: 0
使用std::array
,如已建议的,还需要在初始化时使用额外的一对{}来传递值。
#include <array>
#include <iostream>
template<typename T, size_t ar_sz>
class Test_class
{
public:
Test_class(const std::array<T,ar_sz>& values) :
m_values{ values }
{
}
const auto& values() const noexcept
{
return m_values;
}
private:
std::array<T, ar_sz> m_values;
};
int main()
{
std::array values{ 1,2,3 };
Test_class test_ar{ values };
std::cout << test_ar.values()[2];
return 0;
}
英文:
Use std::array as already suggested, also when initializing you need an extra pair of {} to pass the values.
#include <array>
#include <iostream>
template<typename T, size_t ar_sz>
class Test_class
{
public:
Test_class(const std::array<T,ar_sz>& values) :
m_values{ values }
{
}
const auto& values() const noexcept
{
return m_values;
}
private:
std::array<T, ar_sz> m_values;
};
int main()
{
std::array values{ 1,2,3 };
Test_class test_ar{ values };
std::cout << test_ar.values()[2];
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论