简体   繁体   English

为什么这个模板类没有编译?

[英]Why is this Template Class Not Compiling?

So I have the following bit of code: 所以我有以下代码:

template <typename Type>
class Delegate
{
public:
    Delegate(Type x)
    {
    }
};

void Method()
{
}

int main()
{
    Delegate d(&Method);
    return 0;
}

My question is: why can't the compiler deduce the template type based on what's passed into the constructor? 我的问题是:为什么编译器不能根据传递给构造函数的内容推断出模板类型? The compile error I get is: Argument list for class template Delegate is missing. 我得到的编译错误是: Argument list for class template Delegate is missing. I understand that, but I thought type inference could overcome this to allow for cleaner syntax. 我理解这一点,但我认为类型推断可以克服这一点,以允许更清晰的语法。

Because template parameter deduction only applies to functions . 因为模板参数扣除仅适用于函数 Class templates require parameters explicitly, always. 类模板总是需要显式参数。

That's why many templates have a "named constructor" a function that simply constructs a temporary instance, but by virtue of being a function template rather than class template deduces parameters. 这就是为什么许多模板都有一个“命名构造函数”,它只是一个简单构造一个临时实例的函数,但由于它是一个函数模板而不是类模板,因此推导出了参数。 For example std::make_pair . 例如std::make_pair

C++11 introduced this new meaning of auto that actually allows you to deduce type of variable. C ++ 11引入了auto这个新含义,实际上允许你推导出变量的类型。 So if you have C++11, you can create a "named constructor" for your class, like: 因此,如果你有C ++ 11,你可以为你的类创建一个“命名构造函数”,例如:

template <typename Type>
Delegate<Type> delegate(Type x) { return Delegate<Type>(x); }

and you can create a variable of deduced type with it like: 你可以创建一个推导类型的变量,如:

auto d = delegate(&Method);

Note, that this deduces d to be exactly the type returned by the initializer (you can have auto & or auto && if you want, but not much beyond that). 请注意,这推断d恰好是初始化程序返回的类型(如果需要,可以使用auto &auto && ,但除此之外不多)。 This is way easier than trying to deduce hypothetical Delegate d(&Method) , because that would involve cyclical dependency between deducing the type depending on overload resolution between constructors and the set of viable constructors depending on the deduced type (remember, constructors can be overloaded and types can be partially specialized). 这比尝试推断假设的Delegate d(&Method)更容易,因为这将涉及在构造函数和可行构造函数集之间根据推导的类型推断类型之间的循环依赖性(记住,构造函数可以重载和类型可以部分专业化)。

It's for the same reason that this won't work: 出于同样的原因,这不起作用:

// attempt to create a std::vector<std::string> of ten "x" strings:
std::vector v(10, "x");

In fact, it should result in the very same error message. 实际上,它应该导致完全相同的错误消息。

Use something like this to use type deduction: 使用这样的东西来使用类型推导:

template <class Type>
Delegate<Type> MakeDelegate(Type const &x)
{
    return Delegate<Type>(x);
}

Or just do as you'd do with a std::vector and declare the type explictly. 或者只是像使用std::vector并明确声明类型。

By the way, main must return int , and arguments of unknown type (ie in templates) should be passed with const& . 顺便说一句, main必须返回int ,并且未知类型的参数(即在模板中)应该与const&传递。

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

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