【问题标题】:Return reference to template vector返回对模板向量的引用
【发布时间】:2014-06-12 18:38:25
【问题描述】:

我从简单的类声明开始,我定义了内联模板方法,该方法返回对特定类型容器的引用。

class JPetParamManager
{
public:
  enum ContainerType {kScintillator, kPM, kKB, kTRB, kTOMB};

  std::vector<JPetScin> fScintillators;
  std::vector<JPetPM>   fPMs;
  std::vector<JPetKB>   fKBs;
  std::vector<JPetTRB>  fTRBs;
  std::vector<JPetTOMB> fTOMBs;

  template <typename T>
  const std::vector<T>& getContainer(const JPetParamManager::ContainerType &p_containerType) const
  {
    switch(p_containerType) 
    {
    case kScintillator:
      return fScintillators;
    case kPM:
      return fPMs;
    case kKB:
      return fKBs;
    case kTRB:
      return fTRBs;
    case kTOMB:
      return fTOMBs;
    }
  }
}

在另一个类方法中,我想从上面的类中返回一些容器:

void JPetAnalysisModuleKB::CreateOutputObjects(const char* outputFilename)
{
  std::vector<JPetKB> l_KBs22 = m_manager.getParamManagerInstance().getContainer<JPetKB>(JPetParamManager::ContainerType::kKB);
}

当我想在 main 中运行此方法时,出现如下错误:

./../../framework/JPetManager/../JPetParamManager/JPetParamManager.h: In member function ‘const std::vector<_RealType>& JPetParamManager::getContainer(const JPetParamManager::ContainerType&) const [with T = JPetKB]’:
JPetAnalysisModuleKB.cpp:55:126:   instantiated from here
./../../framework/JPetManager/../JPetParamManager/JPetParamManager.h:81:14: error: invalid initialization of reference of type ‘const std::vector<JPetKB>&’ from expression of type ‘const std::vector<JPetScin>’
./../../framework/JPetManager/../JPetParamManager/JPetParamManager.h:83:14: error: invalid initialization of reference of type ‘const std::vector<JPetKB>&’ from expression of type ‘const std::vector<JPetPM>’
./../../framework/JPetManager/../JPetParamManager/JPetParamManager.h:87:14: error: invalid initialization of reference of type ‘const std::vector<JPetKB>&’ from expression of type ‘const std::vector<JPetTRB>’
./../../framework/JPetManager/../JPetParamManager/JPetParamManager.h:89:14: error: invalid initialization of reference of type ‘const std::vector<JPetKB>&’ from expression of type ‘const std::vector<JPetTOMB>’
make: *** [JPetAnalysisModuleKB.o] Błąd 1

【问题讨论】:

  • 编译器说的是返回类型(即右侧)与std::vector&lt;JPetKB&gt; l_KBs22(即左侧)不匹配。
  • 仅仅因为您说getContainer&lt;JPetKB&gt;(...) 并不意味着编译器可以忽略其余case 语句中的类型不匹配!
  • 模板参数和函数参数不是多余的吗?

标签: c++ templates vector reference


【解决方案1】:

简介

即使只有一个 switch-labels 会匹配并执行,与其他语句关联的语句必须仍然有效。

编译器试图告诉您,当返回 std::vector&lt;T&gt; const&amp;(其中 T 是传递给您的函数的类型)时,并非所有返回都可以使用。


说明

以下实例化getContainer 的方式使其返回std::vector&lt;PetKB&gt;,但在实例化函数时编译器会看到kScintillator 匹配的case-label em> 返回类型为 std::vector&lt;JPetScin&gt;

m_manager.getParamManagerInstance().getContainer<JPetKB> (JPetParamManager::kScintillator)

由于std::vector&lt;JPetScin&gt; 无法转换为std::vector&lt;PetKB&gt;,编译器会抱怨并基本上说您的代码格式不正确。

即使 switch-condition 没有选择返回类型不同的 case,这同样适用; 所有路径都必须能够返回,否则应用程序格式错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 2019-08-19
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多