是否可以就地构造一个固定大小的数组作为函数参数?
Is it possible to construct a fixed sized array in-place as a function argument?
这主要是关于与固定大小数组相关的C++语法的一角的问题。
假设我有一个利用类型信息的函数,例如:
template<class T> void fun(T const& t){
std::cout << typeid(t).name() << std::endl;
}
我可以传递一个值或一个临时对象:
int i;
fun(i); // prints "int" ("i" actually)
fun(int{}); // prints "int" ("i" actually)
但是我不能对数组做同样的事情
double a[10][10];
fun(a); // ok, prints "a[10][10]" ("A10_A10_d" actually)
fun(double[10][10]); // doesn't compile
fun(double{}[10][10]); // doesn't compile
fun(double[10][10]{}); // doesn't compile
fun(double()[10][10]); // doesn't compile
fun(double[10][10]()); // doesn't compile
fun(double(&)[10][10]); // doesn't compile
fun(double(*)[10][10]); // doesn't compile
原则上我可以做到:
typedef double a1010[10][10];
fun(a1010{});
但是,是否可以不预定义 typedef?
是否可以就地构造一个固定大小的数组作为函数参数?
完整代码:
template<class T> void fun(T const& t){
std::cout << typeid(t).name() << std::endl;
}
typedef double a1010[10][10];
int main(){
int i;
fun(i); // prints "int" ("i" actually)
double a[10][10];
fun(a); // prints "a[10][10]" ("A10_A10_d" actually)
fun(a1010{});
fun(int{}); // prints "int"
/* fun(double[10][10]); // doesn't compile
fun(double{}[10][10]); // doesn't compile
fun(double[10][10]{}); // doesn't compile
fun(double()[10][10]); // doesn't compile
fun(double[10][10]()); // doesn't compile
fun(double(&)[10][10]); // doesn't compile
fun(double(*)[10][10]); // doesn't compile
*/
return 0;
}
奖励积分(可能是赏金(:可变大小的数组呢?
int N = 10;
f(double[N]);
尝试:
fun((int[3]){1,2,3});
fun((int[5]){});
至于"奖励点":可变大小的数组不是语言的一部分。此语言扩展不适用于模板参数:
prog.cc:4:6: 注意:候选模板被忽略:替换失败: 可变修改的类型"int [n]"不能用作模板参数 乐趣(常量T&T(
编辑
正如Chris所指出的,上述解决方案建议使用复合文字,这是对C++的扩展。有一种解决方案可以使用简单的帮助程序类来避免此扩展C++:
template <class T, std::size_t N>
struct my_array
{
T data[N];
};
template <class T, std::size_t N>
void print(const T (&x)[N])
{
for (auto i: x)
std::cout << i << 'n';
}
int main()
{
print(my_array<int,3>{9,10,11}.data);
}
这很好用,但需要将模板参数添加到my_array,这是无法推断的。使用 C++17,可以自动推断类型和大小:
template <class T, std::size_t N>
struct my_array
{
constexpr my_array(std::initializer_list<T> x)
{
std::size_t i = 0;
for (auto val : x)
data[i++] = val;
}
T data[N];
};
template <class ...T>
my_array(T...) -> my_array<typename std::common_type<T...>::type, sizeof...(T)>;
int main()
{
print(my_array{9,10,11}.data);
}
对于二维数组,这稍微复杂一些:
template <class T, std::size_t N1, std::size_t N2>
struct my_array2d
{
constexpr my_array2d(std::initializer_list<std::initializer_list<T> > x)
{
std::size_t i = 0;
for (const auto & row : x) {
int j=0;
for (const auto & val: row) {
data[i][j++] = val;
}
i++;
}
}
T data[N1][N2];
};
int main()
{
work(my_array2d<int, 3, 2>{{9,1},{10,2},{11,3}}.data);
}
我已经放弃了二维数组的演绎指南,但我相信它们是可能的。
你已经尝试了很多double
的组合,但你似乎错过了一个。
fun((double[10][10]){});
这将编译并给出:A10_A10_d
相关文章:
- C++函数模板需要 &for 数组参数
- C++ 数组参数不起作用
- 使用数组参数进行函数专用化
- 为什么我的数组参数的方法无法正常工作?(C )
- SWIG - Java 代理类数组参数
- 在 gcc 中使用数组参数编译外部"c"代码
- 从 VB6 调用的 C++ DLL 函数中的输出数组参数
- 如何根据数组参数项类型重载 IDL 中的函数
- 引用谷歌测试/模拟框架中的数组参数
- C++将数组参数传递给函数
- 字符星形数组参数未正确终止
- 如何将德尔菲的"字符串数组"参数翻译成C++?
- 为什么 gcc 无法推断数组参数的模板化大小?(C++11)
- 使用 const char 数组参数分析 constexpr 显示运行时执行
- C++指针数组参数
- 固定大小的多维数组参数
- 为什么数组参数被视为常量数组
- 如何在pgsql中格式化数组参数
- C++字符数组参数在x86编译中包含奇怪的字符
- 对象数组参数 - 错误:字段“字母”的类型不完整