C++ 是否可以使用显式类型在标头中定义(成员)函数并使用auto实现它?

C++ 是否可以使用显式类型在标头中定义(成员)函数并使用auto实现它?,c++,C++,我有一个定义类foo的头foo.h: class Foo { ... std::shared_ptr<std::vector<std::string>> GetVectorPtr(const std::map<std::string, int>& filter, float error); ... } 有可能这样做吗?有可能在C++20或其他未来版本中使用它吗?您可以使用typedef或使用关键字来完成它 使用VectorPt

我有一个定义类foo的头foo.h:

class Foo
{
    ...
    std::shared_ptr<std::vector<std::string>> GetVectorPtr(const std::map<std::string, int>& filter, float error);
    ...
}

有可能这样做吗?有可能在C++20或其他未来版本中使用它吗?

您可以使用
typedef
使用
关键字来完成它

使用VectorPtr=std::shared\u ptr;
使用FilterType=std::map;
使用ErrorType=float;
福班
{
...
VectorPtr GetVectorPtr(常量过滤器类型&过滤器,错误类型错误);
...
}
在定义中

VectorPtr Foo::GetVectorPtr(常量过滤器类型&过滤器,错误类型错误)
{
//工作
}

如果您使用或之类的工具在源代码(或标题,如果需要)中为您创建函数定义,则可以选择此选项。

typedef std::shared\u ptr shared\u vec\u ptr,然后在两个位置都使用它。这就是
typedef
的用途。或者只使用intellisense(如果您使用Visual Studio)为您创建函数定义。这似乎只适用于尾部返回类型,这与以下要点不符:<代码>使用
将是您的最佳选择。以一种方式编写声明,以另一种方式编写定义只会导致混淆。另一方面,编写定义和声明都是代码重复,这有其自身的缺点,比如更难重构。
auto Foo::GetVectorPtr(auto filter, auto error)
{
    // do work
}