c++ - std::array<T,N> 需要在其他类型中获取

标签 c++ arrays c++11

我有一个类,它有一个 std::array 作为成员变量,如下所示:

class my_class{
std::array<point<float>,4> corners;
}

我需要通过这些 corners许多功能。这些函数接受 (point<T>* const) .所以,我只是传递 corners.data() .问题是当某些函数接受特殊的 type 时的 point类(class)。例如,(point<double>* const) .我应该如何以尽可能少的性能开销将角落传递给该函数。

代码:

class my_class{
public:
std::array<point<float>,4> corners;
}
void first_method(point<float>* const input);
void second_method(point<float>* const input);

int main(){
    my_class foo;
    first_method(foo.corners.data()); // OK  
    second_method(foo.corners.data()); // Problem
}

编辑:

我刚刚意识到该类(class)与我的问题无关,我们可以将其简化为:

void first_method(point<float>* const input);
void second_method(point<float>* const input);

int main(){
    std::array<point<float>,4> corners;
    first_method(corners.data()); // OK  
    second_method(corners.data()); // Problem
}

最佳答案

你不能。

您的数组类型错误,或者函数的参数类型错误。

要么修复数组类型,要么修复函数,要么创建第二个数组。

关于c++ - std::array<T,N> 需要在其他类型中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34435578/

相关文章:

java - C++/ java : Efficiently find a set in the collection containing given value

c++ - OpenCV:如何将多条线合并为一条曲线

javascript - 计算数组中的元素并将它们添加到对象中

c++ - 在参数中移出的智能指针上调用方法是否安全?

c++ - SDL_MOUSEBUTTONUP 什么时候触发?

c++ - 为什么这个 C++ 输出与我预期的不同?

c++ - 为什么 char 指针变量可以初始化为字符串,而 int 指针变量不能初始化为整数数组?

php - 如何使用 PHP 在 MySQL 中插入嵌套数组

c++ - 是否可以调用注入(inject)的 friend 模板函数?

c++ - 这种获取模板参数包中最后一个元素的方法是否有隐藏的开销?