如何编写传递大小可变的匿名 std::array 的推导指南?
How to write a deduction guide for passing anonymous std::array of variable size?
我希望能够将可变大小的匿名 std::array
传递给构造函数:
using namespace std;
template <size_t N>
struct A
{
array<string, N> const value;
A (array<string, N>&& v): value {v} {}
};
正在尝试传递这样的匿名数组……
A a { { "hello", "there" } };
这会导致以下错误(使用 clang++):
error: no viable constructor or deduction guide for deduction of template arguments of 'A'
如果我不使用模板并在定义中指定数组的大小,它可以工作:
struct A
{
array<string, 2> const value;
A (array<string, 2>&& v): value {v} {}
};
A a { { "hello", "there" } };
这个案例能不能写个推导指南?如果可以,怎么做?
如果没有,我还能如何解决这个问题?
这适用于我的 gcc 11 -std=c++17
:
#include <string>
#include <array>
using namespace std;
template <size_t N>
struct A
{
array<string, N> const value;
A (array<string, N>&& v): value {v} {}
};
template<typename T, size_t N>
A( T (&&) [N]) -> A<N>;
A a { { "hello", "there" } };
我希望能够将可变大小的匿名 std::array
传递给构造函数:
using namespace std;
template <size_t N>
struct A
{
array<string, N> const value;
A (array<string, N>&& v): value {v} {}
};
正在尝试传递这样的匿名数组……
A a { { "hello", "there" } };
这会导致以下错误(使用 clang++):
error: no viable constructor or deduction guide for deduction of template arguments of 'A'
如果我不使用模板并在定义中指定数组的大小,它可以工作:
struct A
{
array<string, 2> const value;
A (array<string, 2>&& v): value {v} {}
};
A a { { "hello", "there" } };
这个案例能不能写个推导指南?如果可以,怎么做?
如果没有,我还能如何解决这个问题?
这适用于我的 gcc 11 -std=c++17
:
#include <string>
#include <array>
using namespace std;
template <size_t N>
struct A
{
array<string, N> const value;
A (array<string, N>&& v): value {v} {}
};
template<typename T, size_t N>
A( T (&&) [N]) -> A<N>;
A a { { "hello", "there" } };