Fix dereference operator of VectorIterator to structures (#8425)

For Vector or Array of structures the dereference operator of an
iterator returns the pointer to the structure. However, IndirectHelper,
which is used in the implementation of this operator, is instantiated
in the way that the IndirectHelper::Read returns structure by value.

This is because, Vector and Array instantiate IndirectHelper with
const T*, but VectorIterator instantiates IndirectHelper with T. There
are three IndirectHelper template definition: first for T, second for
Offset<T> and the last one for const T*. Those have different
IndirectHelper:Read implementations and (more importantly) return type.
This is the reason of mismatch in VectorIterator::operator* between
return type declaration and what was exactly returned.

That is, for Array<T,...> where T is scalar the VectorIterator is
instantiated as VectorIterator<T, T>, dereference operator returns T
and its implementation uses IndirectHelper<T> which Read function
returns T.
When T is not scalar, then VectorIterator is instantiated as
VectorIterator<T, const T *>, dereference operator returns const T * and
its implementation uses IndirectHelper<T> which Read function returns T.

The fix is done as follows:
* implement type trait is_specialization_of_Offset and
 is_specialization_of_Offset64,
* change partial specialization of IndirectHelper with const T * that
 it is instantiated by T and enabled only if T is not scalar and not
 specialization of Offset or Offset64,
* remove type differentiation (due to scalar) from Array..

The above makes the IndirectHelper able to correctly instantiate itself
basing only on T. Thus, the instantiation in VectorIterator correctly
instantiate IndirectHelper::Read function, especially the return type.
diff --git a/include/flatbuffers/array.h b/include/flatbuffers/array.h
index f4bfbf0..68c245d 100644
--- a/include/flatbuffers/array.h
+++ b/include/flatbuffers/array.h
@@ -31,13 +31,10 @@
   // Array<T> can carry only POD data types (scalars or structs).
   typedef typename flatbuffers::bool_constant<flatbuffers::is_scalar<T>::value>
       scalar_tag;
-  typedef
-      typename flatbuffers::conditional<scalar_tag::value, T, const T *>::type
-          IndirectHelperType;
 
  public:
   typedef uint16_t size_type;
-  typedef typename IndirectHelper<IndirectHelperType>::return_type return_type;
+  typedef typename IndirectHelper<T>::return_type return_type;
   typedef VectorConstIterator<T, return_type, uoffset_t> const_iterator;
   typedef VectorReverseIterator<const_iterator> const_reverse_iterator;
 
@@ -50,7 +47,7 @@
 
   return_type Get(uoffset_t i) const {
     FLATBUFFERS_ASSERT(i < size());
-    return IndirectHelper<IndirectHelperType>::Read(Data(), i);
+    return IndirectHelper<T>::Read(Data(), i);
   }
 
   return_type operator[](uoffset_t i) const { return Get(i); }
diff --git a/include/flatbuffers/buffer.h b/include/flatbuffers/buffer.h
index 94d4f79..00c07c7 100644
--- a/include/flatbuffers/buffer.h
+++ b/include/flatbuffers/buffer.h
@@ -20,6 +20,7 @@
 #include <algorithm>
 
 #include "flatbuffers/base.h"
+#include "flatbuffers/stl_emulation.h"
 
 namespace flatbuffers {
 
@@ -36,6 +37,10 @@
   bool IsNull() const { return !o; }
 };
 
+template<typename T> struct is_specialisation_of_Offset : false_type {};
+template<typename T>
+struct is_specialisation_of_Offset<Offset<T>> : true_type {};
+
 // Wrapper for uoffset64_t Offsets.
 template<typename T = void> struct Offset64 {
   // The type of offset to use.
@@ -48,6 +53,10 @@
   bool IsNull() const { return !o; }
 };
 
+template<typename T> struct is_specialisation_of_Offset64 : false_type {};
+template<typename T>
+struct is_specialisation_of_Offset64<Offset64<T>> : true_type {};
+
 // Litmus check for ensuring the Offsets are the expected size.
 static_assert(sizeof(Offset<>) == 4, "Offset has wrong size");
 static_assert(sizeof(Offset64<>) == 8, "Offset64 has wrong size");
@@ -90,7 +99,7 @@
 // return type like this.
 // The typedef is for the convenience of callers of this function
 // (avoiding the need for a trailing return decltype)
-template<typename T> struct IndirectHelper {
+template<typename T, typename Enable = void> struct IndirectHelper {
   typedef T return_type;
   typedef T mutable_return_type;
   static const size_t element_stride = sizeof(T);
@@ -135,10 +144,20 @@
 };
 
 // For vector of structs.
-template<typename T> struct IndirectHelper<const T *> {
-  typedef const T *return_type;
-  typedef T *mutable_return_type;
-  static const size_t element_stride = sizeof(T);
+template<typename T>
+struct IndirectHelper<
+    T, typename std::enable_if<
+           !std::is_scalar<typename std::remove_pointer<T>::type>::value &&
+           !is_specialisation_of_Offset<T>::value &&
+           !is_specialisation_of_Offset64<T>::value>::type> {
+ private:
+  typedef typename std::remove_pointer<typename std::remove_cv<T>::type>::type
+      pointee_type;
+
+ public:
+  typedef const pointee_type *return_type;
+  typedef pointee_type *mutable_return_type;
+  static const size_t element_stride = sizeof(pointee_type);
 
   static return_type Read(const uint8_t *const p, const size_t i) {
     // Structs are stored inline, relative to the first struct pointer.
diff --git a/tests/test.cpp b/tests/test.cpp
index af8cd63..a374da5 100644
--- a/tests/test.cpp
+++ b/tests/test.cpp
@@ -834,6 +834,24 @@
   TEST_EQ(arr_struct.e(), 10);
   TEST_EQ(arr_struct.f()->Get(0), -2);
   TEST_EQ(arr_struct.f()->Get(1), -1);
+
+  // Test for each loop over NestedStruct entries
+  for (auto i : *arr_struct.d()) {
+    for (auto a : *i->a()) {
+      TEST_EQ(a, 1);
+      break;  // one iteration is enough, just testing compilation
+    }
+    TEST_EQ(i->b(), MyGame::Example::TestEnum::B);
+    for (auto c : *i->c()) {
+      TEST_EQ(c, MyGame::Example::TestEnum::A);
+      break;  // one iteration is enough, just testing compilation
+    }
+    for (auto d : *i->d()) {
+      TEST_EQ(d, -2);
+      break;  // one iteration is enough, just testing compilation
+    }
+    break;  // one iteration is enough, just testing compilation
+  }
 }
 #else
 void FixedLengthArrayConstructorTest() {}