这个汽车的返回类型是什么?

时间:2015-02-18 01:39:17

标签: c++11

遗漏了一些代码,SOF上的其他地方有代码如下:

// 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...);
}

在main.cpp

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

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

我需要打破BindObservers和MakeSubject的返回类型,但我无法在下面的伪代码中正确找出替换** auto 的内容:**

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

以上两个auto的exapanded版本返回类型是什么?我需要将返回值存储在std :: vector和AFAIK中,我不能说

std::vector<auto> vec

[虽然我不明白为什么不这样,因为编译器可能会弄明白]

1 个答案:

答案 0 :(得分:1)

您可以使用std::vector<decltype(pressure)>

但类型应为Pressure<Printer, Printer>