0

If I have some 3D container, which can be the combination of any containing types out there. and I have to write a generic function which receives this container as an argument, and has to return a 2D container (a matrix) that is the same type as the elements of the 3D container.

For expample, the 3D container received:

std::vector<std::deque<std::deque<int>>> 3D;

The returned type:

std::deque<std::deque<int>>

How can I achieve this, and make the return type generic and dependent on the multidimensional container that has been received? Lets say I have a generic function that receives a 3D container, and has to return the same type that the 3D container contains. For example: my function is like this:

template<typename 3D>  
 auto Function(3D Knt)-> decltype(0+Knt[0]){
// randome code
decltype(0+Knt[0]) K;
// randome code
return K;
}

Will this cause any errors?

Nane
  • 55
  • 6
  • 1
    Standard containers declare a member `value_type` type for this purpose. – user7860670 Apr 17 '17 at 19:21
  • I'm confused: You seem to know about `decltype`, so you should be able to work how to get the type. – Rakete1111 Apr 17 '17 at 19:22
  • Yess, I know of it, but i dont know how to target the element, or the element of the element of the 3D container. I tried it like this: auto Function(kontainer 3D) ->decltype(0+3D[0]){} but it doesn't seem to work – Nane Apr 17 '17 at 19:59
  • 1
    `3D` is not a valid identifier. – aschepler Apr 17 '17 at 21:18

1 Answers1

1

There's a typedef inside all STL containers that gives you that information. Of course, you can forget about printing that type, but you can use it. (you can play RTTI games to print the name of the type, but it's compiler dependent and requiring that is an indicator of bad design).

So, if you have:

std::deque<int> myArray;

then you can use

std::deque<int>::value_type one_element;

and now one_element will be of type int.

On the other hand, you can use myArray itself to extract that information like this:

decltype(myArray)::value_type one_element;

and again, one_element is of type int.

Edit: I'm not sure I understand the problem. But if you're sure that this is a 3D container, then the deepest element type can be extracted as follows:

If you have:

std::vector<std::vector<std::vector<int> > > 3dArray;

then

decltype(3dArray)::value_type::value_type::value_type one_element;

will be of type int. If you use this on an array that's not 3D, it won't compile.

Community
  • 1
  • 1
The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • Lets say I have a generic function that recives a 3D container, and has to return the same type that the 3D container containes. For example: my function is like this: 'template ' 'auto Function(3D Knt)-> decltype(0+Knt[0]){' '// randome code' 'decltype(0+Knt[0]) K;' '// randome code' 'return K;' '}' Is this alright? Will this work? – Nane Apr 17 '17 at 20:10
  • @Nane Two things: 1. Add this to your question, I can't understand here. 2. Try it before asking "will this work" and ask about the problem when it doesn't, not about whether it's gonna work. – The Quantum Physicist Apr 17 '17 at 20:31
  • I added it. I am sorry for bothering you, this is basically how my function looks like (simplified), and I did try it beforehand, but the compiler is reporting errors (a lot of them, and some point to this part), and I just want to know if this is a proper way to do this or if I made a mistake because I never used it before and my knowledge of its use is a bit shaky. Not trying to be lazy, I am just a beginner. – Nane Apr 17 '17 at 20:39
  • @Nane I'm not familiar with the `auto` return, but I gave you how to extract the inner-most type from a 3D array. I gotta go sleep, so I'll continue tomorrow. Try and see whether you can get what you want with this information. Good luck! – The Quantum Physicist Apr 17 '17 at 20:46