- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <type_traits>
template<typename T>
using Ref1 = T & ;
template<typename T>
using Ref2 = std::add_lvalue_reference_t<T>;
template<typename T>
void f1(Ref1<T>)
{}
template<typename T>
void f2(Ref2<T>)
{}
int main()
{
int n{};
f1(n); // ok
f2(n); // error
}
我的编译器是clang 7.0,用c++17
编译。错误信息是:
error : no matching function for call to 'f2'
note: candidate template ignored:
couldn't infer template argument 'T'
为什么 f1
可以,但 f2
不行?
最佳答案
std::add_lvalue_reference_t<T>
定义为 typename std::add_lvalue_reference<T>::type
, 然后为 template<typename T> void f2(Ref2<T>)
,即 template<typename T> void f2(typename std::add_lvalue_reference<T>::type)
, 属于 non-deduced context ,这会导致模板参数推导失败。
In the following cases, the types, templates, and non-type values that are used to compose P do not participate in template argument deduction, but instead use the template arguments that were either deduced elsewhere or explicitly specified. If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.
1) The nested-name-specifier (everything to the left of the scope resolution operator ::) of a type that was specified using a qualified-id:
关于c++ - 为什么 std::add_lvalue_reference 的行为不如预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679047/
我有这样一个类,它为 montecarlo 模拟器创建路径,它需要从可用整数数组创建整数路径。因此,例如,我们可以从包含 {0,1,2,3,4} 的数组中提取长度为 3 的路径,这将生成 3,1,2
#include template using Ref1 = T & ; template using Ref2 = std::add_lvalue_reference_t; template vo
cppreference.com关于std::add_lvalue_reference/std::add_rvalue_reference: If T is an object type or a f
std::add_lvalue_reference 和 std::add_rvalue_reference 的用途是什么? 看起来使用 T &/T && 的效果是一样的,因为这成功编译了: #incl
将类型更改为类型的reference,允许访问该类型的成员而无需创建该类型的实例。 lvalue references 和 rvalue references 似乎都是如此。 declval 是用 a
我是一名优秀的程序员,十分优秀!