简体   繁体   English

为什么编译器不能从返回类型中推导出模板参数?

[英]Why can't the compiler deduce template parameter from return type?

Given the following code 给出以下代码

#include <vector>
#include <memory>

using namespace std;

class MyBase
{};

class MyDerived : public MyBase
{};

template<class Base, class Derived>
vector<Base> makeBaseVec(const Derived& obj, const typename vector<Base>::size_type size)
{
    vector<Base> out;
    for (typename vector<Base>::size_type i = 0; i < size; i++)
    {
        out.push_back(Base(obj) /* copy constructor */);
    }

    return out;
}

int main()
{
    MyDerived a;
    vector<MyBase> v = makeBaseVec<MyBase>(a, 10);
}

Live example 实例

Why do I get the error 为什么我会收到错误

main.cpp:13:14: note:   template argument deduction/substitution failed:
main.cpp:29:41: note:   couldn't deduce template parameter 'Base'
     vector<MyBase> v = makeBaseVec(a, 10);
                                         ^

Shouldn't the compiler be able to deduce the the template parameter Base from the type of v ? 编译器是否应该能够从v的类型中推导出模板参数Base

I can rectify this by changing line 27 to 我可以通过改变第27行来纠正这个问题

vector<MyBase> v = makeBaseVec<MyBase>(a, 10);

but this felt unnecessary. 但这感觉不必要。

Shouldn't the compiler be able to deduce the the template parameter Base from the type of v ? 编译器是否应该能够从v的类型中推导出模板参数Base?

The type of v is not considered by the template type deduction mechanism when you call makeBaseVec . 调用makeBaseVec时,模板类型推导机制不考虑v的类型。 What if you were to call the function and discard the return value? 如果您要调用该函数并丢弃返回值,该怎么办?

Return types do not participate in type deduction or overload resolution. 返回类型不参与类型推导或重载解析。

If you don't want to repeat yourself, you can use type deduction on v instead: 如果您不想重复自己,可以在v上使用类型推导:

auto v = makeBaseVec<MyBase>(a, 10);

In fact, almost always auto is a good policy for variables. 事实上,几乎所有auto都是变量的好政策。

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

相关问题 如果参数类型取决于模板参数,为什么编译器不能推断返回类型 - Why can't the compiler deduce the return type if argument type depends on a template parameter 为什么编译器不能从默认参数推导出模板类型? - Why can't the compiler deduce the template type from default arguments? 为什么 GCC 编译器不能以别名模板形式从 std::array 推导出模板参数之一 - Why can't GCC compiler deduce one of the template parameter from std::array in alias template form 编译器不能推导出返回类型? - Compiler can't deduce the return type? 为什么编译器无法推断出返回类型? - Why can't compiler deduce the return types? 如何“帮助”编译器从作为函数的模板参数中推断出函数模板的返回类型? - How to 'help' the compiler to deduce function template return type from a template parameter which is a function? 为什么C ++不能从赋值中推断出模板类型? - Why can't C++ deduce template type from assignment? 为什么在零数组的情况下编译器不推导template参数? - Why a compiler doesn't deduce the template parameter in case of zero array? 如何让编译器推导出模板的返回类型? - How can I let the compiler deduce the return type of a template? 为什么编译器在与转换运算符一起使用时不能推导出模板参数? - Why can't the compiler deduce the template parameter when used with a conversion operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM