Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么std::add_lvalue_引用的行为不符合预期?_C++_C++11_Standards_Typetraits_Using Declaration - Fatal编程技术网

C++ 为什么std::add_lvalue_引用的行为不符合预期?

C++ 为什么std::add_lvalue_引用的行为不符合预期?,c++,c++11,standards,typetraits,using-declaration,C++,C++11,Standards,Typetraits,Using Declaration,为什么f1可以,而f2不行?将定义为typename std::add\u lvalue\u reference::type,然后对于模板void f2(Ref2),即模板void f2(typename std::add\u lvalue\u reference::type),这会导致模板参数推断失败 在以下情况下,用于组合p的类型、模板和非类型值不参与模板参数推导,而是使用在别处推导或显式指定的模板参数。如果模板参数仅在非推导上下文中使用且未显式指定,则模板参数推导将失败 1) 使用限定id

为什么
f1
可以,而
f2
不行?

将定义为
typename std::add\u lvalue\u reference::type
,然后对于
模板void f2(Ref2)
,即
模板void f2(typename std::add\u lvalue\u reference::type)
,这会导致模板参数推断失败

在以下情况下,用于组合p的类型、模板和非类型值不参与模板参数推导,而是使用在别处推导或显式指定的模板参数。如果模板参数仅在非推导上下文中使用且未显式指定,则模板参数推导将失败

1) 使用限定id指定的类型的嵌套名称说明符(范围解析运算符左侧的所有内容::):

#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
}
error : no matching function for call to 'f2'
  note: candidate template ignored:
        couldn't infer template argument 'T'