How to initialize class object with array reference passed as a template argument to constructor in C++ 17 using braced initializator?

huangapple go评论86阅读模式
英文:

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&lt;typename T, size_t ar_sz&gt; class Test_class {
    // Thats incorrect
    /*
    T t;
    size_t size;
    */
    
    // Thats correct?
    T array[ar_sz];
public:

    Test_class(const T (&amp;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&quot;test&quot; to a constructor, which is a const wchar_t[5], so this task cant be resolved using std::array&lt;&gt;

答案1

得分: 2

编译器推断 T = intar_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 &lt;iostream&gt;
#include &lt;algorithm&gt;

template&lt;typename T, size_t size&gt; 
struct Test
{
public:
    T m_values[size]; // array, not element (you could also use std::array)

    Test(const T (&amp;values)[size])
    {
    	std::copy(values, values + size, m_values); // explicit copying (you could also use memcpy)
    }

    const auto&amp; values() const noexcept
    {
        return m_values;
    }
};

int main() 
{
    Test test(&quot;meh&quot;);

    std::cout &lt;&lt; test.values()[1] &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; test.values()[2] &lt;&lt; &#39;\n&#39;;
    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 &lt;array&gt;
#include &lt;iostream&gt;

template&lt;typename T, size_t ar_sz&gt; 
class Test_class 
{
public:
    Test_class(const std::array&lt;T,ar_sz&gt;&amp; values) : 
        m_values{ values } 
    {
    }

    const auto&amp; values() const noexcept
    {
        return m_values;
    }

private:
    std::array&lt;T, ar_sz&gt; m_values;

};

int main() 
{
    std::array values{ 1,2,3 };
    Test_class test_ar{ values };     

    std::cout &lt;&lt; test_ar.values()[2];
    return 0;
}

huangapple
  • 本文由 发表于 2023年6月27日 21:55:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定