繁体   English   中英

使用模板类的嵌套类型作为模板参数

[英]using nested-types of a template-class as template parameter

我想使用模板类的嵌套类型来实现模板功能。

我刚刚在这里读到,最好将operator <<实现为非成员和非朋友函数。 因此,我决定将函数toStream() MyClass之外的toStream()tableToStream()

template <typename T>
class MyClass
{
public:
  typedef boost::dynamic_bitset<> BoolTable; 
  typedef std::vector<T>          MsgTable;
private:
  BoolTable  b_;
  MsgTable   m_;
public:
  const BoolTable& getB() const { return b_; }
  const MsgTable & getM() const { return m_; }

  std::ostream& toStream (std::ostream& os) const
  {
    os <<"Bool: ";  tableToStream (os, getB());  os <<'\n';
    os <<"Msg:";    tableToStream (os, getM());  os <<'\n';
    return os;
  }

  template <typename TABLE>
  std::ostream& tableToStream (std::ostream& os, const TABLE& table) const
  {
    for (int i=0; i < table.size(); ++i)
      os << table[i] <<',';
    return os;
  }
};

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T> mc)
{
  return mc.toStream(os);
}

MyClass::toStream()转换为operator <<非成员和非朋友函数很容易:

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)
{
  os <<"Bool: ";  mc.tableToStream (os, mc.getB());  os <<'\n';
  os <<"Msg:";    mc.tableToStream (os, mc.getM());  os <<'\n';
   return os;
}

但是我只想使用operator <<而不是调用MyClass::tableToStream()

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)
{
  os <<"Bool: "<< mc.getB() <<'\n';
  os <<"Msg:"  << mc.getM() <<'\n';
   return os;
}

对于函数MyClass::tableToStream()我可以使用以下实现,但是由于该函数太通用(任何类型都可以是TABLE ),所以这可能会使流输出混乱。

template <typename TABLE>
std::ostream& operator << (std::ostream& os, const TABLE& table) 
{
  for (int i=0; i < table.size(); ++i)
    os << table[i] <<',';
  return os;
}

因此,我想限制MyClass嵌套类型 以下是我将MyClass::tableToStream()转换为标准operator <<非成员和非朋友函数的尝试之一:

template <typename T, typename MyClass<T>::TABLE>
std::ostream& operator << (std::ostream& os, const TABLE& table) 
{
  for (int i=0; i < table.size(); ++i)
    os << table[i] <<',';
  return os;
}

但是错误是关于typename MyClass<T>::TABLE

既然您已经澄清了很多问题,那么我的第一个答案将不再适用,我将对其进行删除-编辑,以便为您提供更好的选择:

更新的答案:您希望将模板限制为仅接受在MyClass模板中键入有效的类型。 通常通过应用SFINAE来实现这种约束,特别是通过std::enable_if (或boost::enable_if ,如果您的库缺少C ++ 11支持的部分)。 可悲的是,没有像is_typedeffed_inside这样的特征可以用于您的案例。 更糟糕的是:仅使用普通的typedef就无法编写这样的特征,因为在给定的类中进行类型化没有什么特别的-编译器无法确定(并且不关心)给定的已知值类型在某处具有一些别名。

但是,如果您的typedef只是您在问题中显示的typedef,那么我对您来说是个好消息:您恰好需要两个operator<<

  1. 一个用于boost::dynamic_bitset<> ,因为它是任何 MyClass实例化的BoolTable。
  2. 另一个模板化为std::vector<T> ,因为这是每个对应的MyClass<T>的MsgTable。

缺点是,使用此模板化的operator<< ,即使FooBar与MyClass的使用完全无关,您也可以输出任何std::vector<FooBar> 但这适用于适当的operator<<的任何其他可能的实现-如果对MSG参数没有明确限制,则对FooBar也没有限制,使std::vector<FooBar>成为可行的MyClass<MSG>::MsgTable

我对您的问题的结论是:您希望使用operator<<以便于查看,因为通常将其用于此目的。 在您的情况下,可以为MyClass<MSG>对象提供它,但是没有办法仅对内部typedef提供它。

我可以这样实现:

template <class MSG>
class MyClass {
  /* ... */
public:
  // instead of declaring op<< a friend, redirect to a method that has 
  // natural access to private members
  std::ostream& printToStream(std::ostream& os) const
  {
    os << "Bool: "; 
    tableToStream (getB(), os); 
    os <<"\nMsg:";   
    tableToStream (getM(), os); 
    return os <<'\n';
  }
private:
  // make this one private so nobody can misuse it to print unrelated stuff
  template <class Table>
  static void tableToStream(Table const& table, std::ostream& os)
  {
    std::copy(begin(table), end(table), ostream_iterator(os, ", "));    
  }
};

template <typename MSG>
std::ostream& operator << (std::ostream& os, const MyClass<MSG>& mc)
{
  return mc.printToStream(os); 
}

你原来的课没问题。 的确,如果您想要一个operator <<来写入流,则它应该像您一样是非成员非朋友函数,但是没有理由该函数不能调用公共成员函数做工作。

我终于找到了类似的问题

就我而言, 解决方案是:

template <typename T>
std::ostream& operator << (std::ostream& os, 
                           typename MyClass<T>::TABLE const& table) 
{
  for (int i=0; i < table.size(); ++i)
    os << table[i] <<',';
  return os;
}

更新:正如@ArneMertz所指出的,上述功能不起作用。
以下是我测试过的完整代码:

#include <ostream>
#include <boost/dynamic_bitset.hpp>

template <typename T>
class MyClass  
{
  public:
    typedef boost::dynamic_bitset<> BoolTable; 
    typedef std::vector<T>          MsgTable;

    BoolTable  b_;
    MsgTable   m_;

    const BoolTable& getB() const { return b_; }
    const MsgTable & getM() const { return m_; }
};

template <typename T>
std::ostream& operator << (std::ostream& os, 
                           typename MyClass<T>::TABLE const& table) 
{
  for (int i=0; i < table.size(); ++i)
    os << table[i] <<',';
  return os;
}

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)  
{
    os <<"Bool: "<< mc.getB() <<'\n'; // <-- this line is OK because it
    os <<"Msg:  "<< mc.getM() <<'\n';            //uses boost operator<<
    return os;
}

main功能:

#include <iostream>

int main()
{
  MyClass<int> var;
  var.b_.push_back(true);
  var.b_.push_back(false);
  var.b_.push_back(true);
  var.m_.push_back(23);
  var.m_.push_back(24);
  var.m_.push_back(25);

  std::cout << var;
}

我相信您会感到困惑。 类型名称只是为了能够将其与其他模板参数分开。 尝试将其重命名为

template <typename OS, typename MSG, typename MSGTable>
OS& operator << (OS& os, const MSGTable& table) const{}

然后像一个对象一样使用它。

这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM