简体   繁体   English

推导出的返回类型依赖于成员 function 模板参数

[英]deduced return type depedent on member function template argument

I am trying to figure out what's wrong with this code:我试图弄清楚这段代码有什么问题:

#include <string>

struct Bar
{
    using Deduced = typename std::string;
};

class Test
{
    template<typename Foo>
    auto Func() ->  decltype(Foo::Deduced)
    {
        return Foo::Deduced();
    }
};

int main()
{
    Test t;
    std::string ret = t.template Func<Bar>();
}

I am refactoring a class where Func return type was declared as a template class parameter of Test class, but I would like to change this to work as a member template parameter.我正在重构一个 class,其中Func返回类型被声明为Test class 的模板 class 参数,但我想将其更改为成员模板参数。

Is this possible?这可能吗? If yes, anyone could suggest me the right syntax?如果是,任何人都可以建议我正确的语法?

Remove the decltype .删除decltype

decltype gives the type of an expression . decltype给出表达式的类型。 But Foo::Deduced is already a type, so you can't apply decltype to it.但是Foo::Deduced已经是一个类型,所以你不能对它应用decltype You simply want to refer to that type itself.您只是想引用该类型本身。 So all you have to do is write Foo::Deduced .所以你所要做的就是写Foo::Deduced

However, in some contexts, you need to prefix it with typename to tell the compiler that it's really a type.但是,在某些情况下,您需要在它前面加上typename以告诉编译器它确实是一个类型。 In C++20, you need typename in return Foo::Deduced() but you don't need it in the trailing return type.在 C++20 中,您需要typenamereturn Foo::Deduced()中,但在尾随返回类型中不需要它。 Prior to C++20, it was needed in both places.在 C++20 之前,这两个地方都需要它。 Thus your code could be rewritten like so:因此,您的代码可以像这样重写:

template<typename Foo>
auto Func() ->  Foo::Deduced
{
    return typename Foo::Deduced();
}

Or a deduced return type could be used:或者可以使用推导的返回类型:

template<typename Foo>
decltype(auto) Func()  // notice no trailing return type
{
    return typename Foo::Deduced();
}

In this alias declaration在这个别名声明中

using Deduced = typename std::string;

the keyword typename is redundant.关键字typename是多余的。 Just write写吧

using Deduced = std::string;

The template function Func is a private member function of the class Test .模板 function Func是 class Test的私有成员 function 。 You need to make it public.您需要将其公开。

The decltype specifier expects an expression. decltype说明符需要一个表达式。 You should write at least like你至少应该写

auto Func() ->  typename Foo::Deduced

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

相关问题 Constexpr类模板成员函数与推导的void返回类型? - Constexpr class template member function with deduced void return type? 使用 function 模板推导出的返回类型 - deduced return type with function template 使用默认模板参数在 function 模板上获取具有自动推导返回类型的指针 - Getting a pointer with auto deduced return type on function template with default template argument 从(const)推导的类型*用作模板函数template参数 - Deduced type from (const) *this used as template function template argument 可以从指向成员函数模板参数的指针推导出类类型 - Can class type be deduced from pointer to member function template parameter 具有返回类型的函数模板,不能从参数推导出 - a function template having a return type, which cannot be deduced from arguments 根据模板参数大小在成员函数中使用不同的返回类型 - Use different return type in member function based on template argument size 在另一个成员函数的尾随返回类型中获得推断成员函数的decltype是否格式良好? - Is getting the decltype of a deduced member function inside the trailing return type of another member function well-formed? 使用 auto 推导的 lambda 成员函数模板 - Template on member function in lambda deduced using auto 以'auto &amp;&amp;'作为 function 返回类型的推导类型 - Deduced type with 'auto &&' as function return type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM