title: How to check for type of struct or if field exists
body: <p>I have the following code to compute the maximum absolute value of a vector of "multiples" (structs representing multiples) - in my case, I have <code>std::pair</code>'s and my own <code>Triple</code> struct which does essentially the same as the former but with 3 fields. </p>
  
  <pre><code>/**
   * @brief Computes the maximum absolute value of a vector of specified structs 
   *
   * Iterates through all elements of a vector checking the T.first, T.second and T.third
   * values to find the abs maximum element of the data structure.
   *
   * @param data Vector of pairs of integers
   * @param elementOfMax Pointer to integer which will store the element (1,2 or 3) that the maximum 
   * value of the vector of T structs is contained within, pass the address of an int variable as this param.
   * @param coordChoice [= 0] Optional argument to choose specific 'x' or 'y' co-ordinate
   * of the T struct to compute maximum for - set coordChoice to 1 for 1st element, 2
   * for 2nd element (etc.) any other value will result in all elements being considered.
   * @return maximum value of data
   */
  template&lt;typename T&gt; int absMaxOfVectorOfMultiples(std::vector&lt; T &gt;&amp; data, int* elementOfMax, int coordChoice = 0) {
  
      // set initial maximum to 0
      int maximum = 0;
      *elementOfMax = 0;
  
      bool isPair = false;
  
      if (typeid(T).name() == typeid(std::pair&lt;int, int&gt;).name()) {
          isPair = true;
      }
  
      // loop over all elements in the data vector
      for (unsigned int i = 0; i &lt; data.size(); i++) {
  
          if (coordChoice != 2 &amp;&amp; coordChoice != 3) {
  
              // if the first element of the multipe struct at this data point
              // is greater than current maximum, set this element
              // to the new maximum value
              if (std::abs(data.at(i).first) &gt; maximum) {
                  maximum = data.at(i).first;
                  *elementOfMax = 1;
              }
          }
  
          if (coordChoice != 1 &amp;&amp; coordChoice != 3) {
  
              // if the second element of the multiple struct at this data point
              // is greater than current maximum, set this element
              // to the new maximum value
              if (std::abs(data.at(i).second) &gt; maximum) {
                  maximum = data.at(i).second;
                  *elementOfMax = 2;
              }
          }
  
          if (!isPair) {
  
              if (coordChoice != 1 &amp;&amp; coordChoice != 2) {
  
                  // if the third element of the multtiple struct at this data point
                  // is greater than current maximum, set this element
                  // to the new maximum value
                  if (std::abs(data.at(i).third) &gt; maximum) {
                      maximum = data.at(i).third;
                      *elementOfMax = 3;
                  }
  
  
              }
  
  
          }
  
      }
  
  return maximum;
  
  }
  </code></pre>
  
  <p>Despite not having tested this yet, I know this will not work when passing <code>std::pair</code> structs to the function as there is no <code>third</code> field in a pair. How would I go about altering this code so that the code block for getting and checking the <code>third</code> field is only "available" and executed if the struct passed is a <code>Triple</code>?</p>