什么是不受干扰的上下文? [英] What is a nondeduced context?

查看:105
本文介绍了什么是不受干扰的上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近暴露了这个问题,答案可以总结为这是一个不受限制的上下文 。

I've been exposed to this question recently and the answers can be summed up to "It's a nondeduced context".

具体来说,第一个说这是一个事情,然后重定向到细节的标准,而第二个引用的标准,这是神秘的说最低限度。

Specifically, the first one says it's such a thing and then redirects to the standard for "details", while the second one quotes the standard, which is cryptic to say the least.

有人可以解释为只是凡人,像我自己,一个非歧异的上下文,是什么时候发生,发生?

Can someone please explain to mere mortals, like myself, what a nondeduced context is, when does it occur, and why does it occur?

推荐答案

很简单: deduction 是指确定模板参数类型的过程给定的参数。它适用于函数模板, auto 和一些其他情况(例如部分专业化)。例如,考虑:

It's very simple: deduction refers to the process of determining the type of a template parameter from a given argument. It applies to function templates, auto, and a few other cases (e.g. partial specialization). For example, consider:

template <typename T> void f(std::vector<T>);



现在如果你说 f(x) ,其中你声明 std :: vector< int> x; ,则 T 被推断
int 并且你得到专业化 f

Now if you say f(x), where you declared std::vector<int> x;, then T is deduced as int, and you get the specialization f<int>.

为了扣除工作,模板参数类型被推断必须出现在可推论的语境中。在这个例子中, f 的函数参数是这样一个可推导的上下文。也就是说,函数调用表达式中的参数允许我们确定模板参数 T 应该是为了使调用表达式有效。

In order for deduction to work, the template parameter type that is to be deduced has to appear in a deducible context. In this example, the function parameter of f is such a deducible context. That is, an argument in the function call expression allows us to determine what the template parameter T should be in order for the call expression to be valid.

但是,还有限定的上下文,其中不能进行扣除。规范示例是出现在 :: 左侧的模板参数:

However, there are also non-deduced contexts, where no deduction is possible. The canonical example is "a template parameter that appears to the left of a :::

template <typename> struct Foo;

template <typename T> void g(typename Foo<T>::type);

在此函数模板中,函数参数列表中的 T 因此,你不能说 g(x)并推导出 T 。原因是没有例如,您可以具有以下专业化:

In this function template, the T in the function parameter list is in a non-deduced context. Thus you cannot say g(x) and deduce T. The reason for this is that there is no "backwards correspondence" between arbitrary types and members Foo<T>::type. For example, you could have specializations:

 template <> struct Foo<int>       { using type = double; };
 template <> struct Foo<char>      { using type = double; };
 template <> struct Foo<float>     { using type = bool; };
 template <> struct Foo<long>      { int type = 10; };
 template <> struct Foo<unsigned>  { };

如果调用 g(double {}) T 有两个可能的答案,如果调用 g(int {}),则没有答案。一般来说,类模板参数和类成员之间没有关系,因此您不能执行任何明智的参数推导。

If you call g(double{}) there are two possible answers for T, and if you call g(int{}) there is no answer. In general, there is no relationship between class template parameters and class members, so you cannot perform any sensible argument deduction.

偶尔,明确禁止参数推论是有用的。这是例如 std :: forward 的情况。另一个示例是,当您从 Foo 转换为 Foo std :: string char const * )。现在假设你有一个免费的函数:

Occasionally it is useful to inhibit argument deduction explicitly. This is for example the case for std::forward. Another example is when you have conversions from Foo<U> to Foo<T>, say, or other conversions (think std::string and char const *). Now suppose you have a free function:

template <typename T> bool binary_function(Foo<T> lhs, Foo<T> rhs);

如果调用 binary_function(t,u),则扣除可能是不明确的,因此失败。但是推断只有一个参数是合理的,并且不是推导出另一个参数,因此允许隐式转换。现在需要一个明确的非推导的上下文,例如像这样:

If you call binary_function(t, u), then the deduction may be ambiguous and thus fail. But it is reasonable to deduce only one argument and not deduce the other, thus permitting implicit conversions. Now an explicitly non-deduced context is needed, for example like this:

template <typename T>
bool binary_function(Foo<T> lhs, typename std::common_type<Foo<T>>::type rhs)
{
    return binary_function(lhs, rhs);
}

(您可能会遇到类似 std :: min(1U,2L)。)

(You may have experienced such deduction problems with something like std::min(1U, 2L).)

这篇关于什么是不受干扰的上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆