是否可以推断引用非类型引用非类型模板参数

是否可以推断引用非类型引用非类型模板参数

本文介绍了是否可以推断引用非类型引用非类型模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我无法工作:

I have the following code, which I cannot get to work:

struct foo {};
foo foo1 = {};

template <foo& F>
class FooClass {};

template <foo& F>
void foobar(FooClass<F> arg) {
}

int main() {
    FooClass<foo1> f;
    foobar(f);
}

错误是:

替换失败:推导的非类型模板参数与其对应的模板参数('foo'vs'foo&')没有相同的类型

note: candidate template ignored: substitution failure : deduced non-type template argument does not have the same type as the its corresponding template parameter ('foo' vs 'foo &')

是否可能推断引用模板参数

推荐答案

这是:

这样会产生错误的结果:

This gives the wrong result for an example like:

template<int &> struct X;
template<int &N> void f(X<N>&);
int n;
void g(X<n> &x) { f(x); }

这里, P X< / code>,其包含< i> i 的类型为 int&
A 的对应值为 n ,这是
<$类型的glvalue c $ c> int
。大概这应该是有效的。

Here, P is X<N>, which contains <i>. The type of i is int&. The corresponding value from A is n, which is a glvalue of type int. Presumably this should be valid.

我认为这个规则意味着像:

I think this rule means to say something like,


如@dyp所述,应该更加宽容。在你的例子中, FooClass< F> F )中的参数没有引用类型 - 键入 foo FooClass 的模板参数是引用。这种差异目前存在争议。

As noted by @dyp, [temp.deduct.type]/17 should be more permissive. In your example, the argument in FooClass<F> (F) does not have reference type - it's an lvalue of type foo. The template parameter of FooClass is a reference. This discrepancy is currently ill-formed.

这篇关于是否可以推断引用非类型引用非类型模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 15:02