Ivan
Ivan

Reputation: 7746

What is the return type of this auto?

With some code left out, elsewhere on SOF there is code that looks like this:

// CRTP Abstract Base class for implementing static subject.
// Example Subclass Usage -- Printing Observer:
   class Printer : public Observer<Printer> {
   public:
     Printer() : timesTriggered_(0) {}
     template <typename... Args>
     void OnNotify(Pressure<Args...> &subject, EventType event) {
       std::cout << "Observer ID: " << this->GetID() << std::endl;
       switch (event) {
       case EventType::UNKNOWN: {
         std::cout << "Unknown Event -- Event #" << timesTriggered_++
                   << std::endl;
         std::cout << "Pressure: " << subject.GetPressure() << std::endl;
         break;
       }
       default: { break; }
       }
     }

   private:
     int timesTriggered_;
   };

// CRTP Abstract Base class for implementing static subject.  
// Example Subclass Usage -- Pressure Sensor:
   template <typename... Obs>
   class Pressure : public Subject<Pressure<Obs...>, Obs...> {
   public:
     typedef Subject<Pressure<Obs...>, Obs...> BaseType;
     Pressure(std::tuple<Obs &...> &&observers, int pressure)
         : BaseType(std::move(observers)), pressure_(pressure) {}
     void Change(int value) {
       pressure_ = value;
       this->NotifyAll(EventType::UNKNOWN);
     }
     int GetPressure() const { return pressure_; }

   private:
     int pressure_;
   };

// Binding function for use with MakeSubject
//   Arguments: observer objects to observe subject notifications
//   Return:    tuple of references to observers
template <typename... Obs> std::tuple<Obs &...> BindObservers(Obs &... obs) {
  return std::tuple<Obs &...>(obs...);
}

// Creator to ease subject creation
//   Template Arguments: Subject subclass type
//   Arguments: Result from BindObservers
//              Any constructor arguments for Subject subclass
//   Return:    Subject subclass
// Example Usage:
// auto pressure = MakeSubject<Pressure>(BindObservers(printerObs), initialPressure);
template <template <typename...> class T, typename... Args, typename... Obs>
T<Obs...> MakeSubject(std::tuple<Obs &...> &&obs, Args &&... args) {
  return T<Obs...>(std::move(obs), args...);
}

In main.cpp

int main() {
  Printer printerObs1;
  Printer printerObs2;
  const int initialPressure = 1;

  auto pressure = MakeSubject<Pressure>(
      BindObservers(printerObs1, printerObs2), initialPressure);
  pressure.Change(12);
}

I need to break out the BindObservers and the return type of MakeSubject, but I can't correctly figure out what to replace both **auto in the pseudo-code below:**

auto obs = BindObservers(printerObs1, printerObs2);
auto pressure = MakeSubject<Pressure>(obs, initialPressure);

What is the exapanded version return types of both auto above? I need to store the return values in std::vector and AFAIK, I can't say

std::vector<auto> vec

[Although I don't see why not since the compiler can probably figure it out]

Upvotes: 0

Views: 56

Answers (1)

StenSoft
StenSoft

Reputation: 9609

You can use std::vector<decltype(pressure)>.

But the type should be Pressure<Printer, Printer>.

Upvotes: 1

Related Questions