简体   繁体   English

我如何使用扣除来选择function的返回类型

[英]How do i use deduction to choose the return type of function

I have a question regarding the full deduction with functions using C++11 standard.我对使用 C++11 标准的函数进行完全推导有疑问。 Basically I am ought to make a function that takes a single parameter that should look like a matrix (basically any container) whose elements can again be any container but having in mind that elements of every row do not necessarily need to be the same size.基本上我应该制作一个 function,它采用一个看起来应该像矩阵(基本上是任何容器)的参数,其元素可以再次是任何容器,但请记住每一行的元素不一定需要相同的大小。 For an example the parameter could be vector of deques, classic C-like matrix, vector of vectors, deques of vectors and so on, you get the point.例如,参数可以是双端队列的向量、经典的类 C 矩阵、向量的向量、向量的双端队列等等,你明白了。 Having said that.话说回来。 Function itself needs to dynamically alocate (using fragmented and not continual allocation) memory for 2d matrix with same structure and type of elements and then copy all the elements from given matrix into it, and finally return a double pointer which you can use to access the elements of matrix. Function 本身需要动态分配(使用碎片而不是连续分配) memory 对于具有相同结构和元素类型的二维矩阵,然后将给定矩阵中的所有元素复制到其中,最后返回一个双指针,您可以使用它来访问矩阵的元素。 Having that said, I do not actually know how to use full deduction so that the function actually knows which type is the double pointer that it must return.话虽如此,我实际上并不知道如何使用完全推导,以便 function 实际上知道哪种类型是它必须返回的双指针。 First thing that crosses my mind is to use something like this:我想到的第一件事是使用这样的东西:

template <typename MatrixType>
auto CreateMatrix (MatrixType M) -> decltype ... 

Logic behind it is that full deduction would find out what type of double pointer must be returned, but the three dots (...) is where im halted, i dont actually know what to write there.它背后的逻辑是完全推导会找出必须返回什么类型的双指针,但三个点 (...) 是我停止的地方,我实际上不知道在那里写什么。

Ofcourse I could probably do something like this:当然我可能会做这样的事情:

template <typename MatrixType>
MatrixType** CreateMatrix (MatrixType M) 

But it doesn't use deduction because call like this:但它不使用扣除,因为这样调用:

std::vector<std::deque<double>> a = {...};
auto x = CreateMatrix(a);

wouldn't work without using <double> with the call of the function, so it is not a full "problem solved" it is just a cheap trick.如果不在 function 的调用中使用<double>将无法工作,所以它不是一个完整的“问题解决”,它只是一个廉价的技巧。

Memory allocation shouldn't be hard, I think It would be pretty easy to do, but currently I'm stuck here and do not know what to do. Memory 分配应该不难,我想这很容易做到,但目前我被困在这里,不知道该怎么办。

I am grateful for every help!我很感激每一个帮助!

I am still not sure if I understood what you want to do.我仍然不确定我是否理解你想做什么。 I will concentrate on the actual question: How to infer the value type from a 2D container when the container can be made of standard containers or C arrays?我将专注于实际问题:当容器可以由标准容器或 C arrays 制成时,如何从二维容器推断值类型?

You can use a type trait.您可以使用类型特征。 Standard containers have a value_type member alias.标准容器有一个value_type成员别名。 When an 2d array is passed as pointer T** then the value type is T .当二维数组作为指针T**传递时,值类型为T When an array T[M][N] is passed by reference then the value type is T :当通过引用传递数组T[M][N]时,值类型为T

template <typename T>
struct value_type { using type = typename T::value_type; };

template <typename T>
struct value_type< T*> { using type = T; };

template <typename T,size_t N>
struct value_type< T[N] > { using type = T;};

template <typename T>
struct value_type2d {
    using type = typename value_type<typename value_type<T>::type>::type;
};

As an example I used a function that simple returns the first element:作为示例,我使用了 function,它简单地返回了第一个元素:

template <typename Matrix>
typename value_type2d<Matrix>::type foo(const Matrix& m) {
    return m[0][0];
}



int main() {
    std::vector<std::vector<double>> x{{42,1},{2,3}};
    std::cout << foo(x) << "\n";
    std::vector<std::deque<double>> y{{42,1},{2,3}};
    std::cout << foo(x) << "\n";
    int z[2][2] = {{42,1},{2,3}};
    std::cout << foo(z) << "\n";
    std::vector<std::string> w[2] = {{"42","1"},{"2","3"}};
    std::cout << foo(x) << "\n";
}

Live Demo现场演示

PS: Yes decltype( M[0][0] ) is a simpler way to get the value type. PS:是的decltype( M[0][0] )是获取值类型的更简单方法。 Though for example std::map::operator[] is not const and you should not pass a non const referene when the function does not modify the parameter.尽管例如std::map::operator[]不是const ,并且当 function 不修改参数时,您不应传递非 const 引用。 And you certainly do not want to copy the whole matrix just to pass it to the function.而且您当然不希望复制整个矩阵只是为了将其传递给 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM