简体   繁体   English

如何从模板化类方法返回依赖类型?

[英]How can I return a dependent type from templated class method?

Let's say I have a class based on a template ThingType . 假设我有一个基于模板ThingType In the header, I use this to typedef a dependent type VectorThingType . 在报头中,我使用它来typedef一个依赖型VectorThingType I'd like to return this from a method GetVectorOfThings() . 我想从方法GetVectorOfThings()返回这个。 If I set VectorThingType as the return type, I get a Does not name a type error, as the type wasn't defined in this scope. 如果我将VectorThingType设置为返回类型,我会得到一个Does not name a type错误,因为该范围中未定义类型。 Is there any way to do this without duplicating the code in the typedef ? 有没有办法在不重复typedef的代码的情况下执行此操作?

#include <vector>
#include <iostream>

template< typename ThingType >
class Thing
{
public:

 ThingType aThing;
 typedef std::vector< ThingType > VectorThingType;
 VectorThingType GetVectorOfThings();

Thing(){};
~Thing(){};

};

template< typename ThingType >
//VectorThingType // Does not name a type 
std::vector< ThingType > // Duplication of code from typedef
Thing< ThingType >
::GetVectorOfThings() {
  VectorThingType v;
  v.push_back(this->aThing);
  v.push_back(this->aThing);
  return v;
}
template< typename ThingType >
auto // <-- defer description of type until...
Thing< ThingType >
::GetVectorOfThings()
-> VectorThingType // <-- we are now in the context of Thing< ThingType >
{
  VectorThingType v;
  v.push_back(this->aThing);
  v.push_back(this->aThing);
  return v;
}

Just came across another answer to this question, which doesn't involve c++11. 刚刚遇到了这个问题的另一个答案,它不涉及c ++ 11。

template< typename ThingType >
typename Thing< ThingType >::VectorThingType
Thing< ThingType >
::GetVectorOfThings()
{
  VectorThingType v;
  v.push_back(this->aThing);
  v.push_back(this->aThing);
  return v;
}

Basically involves assuring the compiler that you are, in fact, dealing with a type via typename and then properly scoping the type using Thing< ThingType >:: . 基本上涉及向编译器确​​保您实际上是通过typename处理类型,然后使用Thing< ThingType >::正确地确定类型。 Could be useful if you are stuck with c++03 for some reason. 如果由于某种原因你被c ++ 03困住可能会有用。

暂无
暂无

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

相关问题 如何从模板化类的方法返回std :: string,无论类型如何? - How to return std::string from method of templated class, no matter the type? 如何使用decltype作为模板化类的返回类型的模板参数? - How can I use decltype as the template parameter for a return type of a templated class? 如何使模板化运算符推导出正确的返回类型? - How can I make templated operator deduce correct return type? 我可以退回不同类型的模板化容器吗? - Can I return a templated container of different type? 在不知道底层类型的情况下,如何从模板化父类取回值? - How can I get the value back from a templated parent class without knowing the underlying type? 如何部分专门化非模板类的模板成员方法? - How can I partially specialise a templated member method of a non-templated class? clang ++自动返回类型错误,专门用于模板化类中的模板化方法? - clang++ auto return type error for specialization of templated method in templated class? 我可以继承模板类并将类型设置为我正在尝试继承的类的模板化子类吗? - Can I inherit a template class and set the type to a templated subclass of the class I'm currently trying to inherit from? 如何基于类型相关类型专门化C ++模板类函数? - How to specialize a C++ templated-class function basing on a type-dependent type? 我如何专门化静态模板类? - How can I specialize a static templated class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM