Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.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++ VS 2013无法根据模板参数专门化具有通用引用和返回类型的函数模板_C++_Visual Studio 2013_Universal Reference - Fatal编程技术网

C++ VS 2013无法根据模板参数专门化具有通用引用和返回类型的函数模板

C++ VS 2013无法根据模板参数专门化具有通用引用和返回类型的函数模板,c++,visual-studio-2013,universal-reference,C++,Visual Studio 2013,Universal Reference,VS 2013表示无法在以下代码中专门化函数模板: struct W { }; template <class T> typename T::result_type f (const W & w, T && t) { return 0; } /* ... */ struct V { typedef int result_type; }; W w {}; V v {}; f (w, v); struct W{}; 模板 typename T::结

VS 2013表示无法在以下代码中专门化函数模板:

struct W { };

template <class T>
typename T::result_type
f (const W & w, T && t) {
    return 0;
}

/* ... */
struct V { typedef int result_type; };

W w {};
V v {};
f (w, v);
struct W{};
模板
typename T::结果类型
f(康斯特水务、水务和水务){
返回0;
}
/* ... */
结构V{typedef int result_type;};
W{};
V{};
f(w,v);
如果我用
int
替换
typename T::result\u type
,或者用
T&
替换通用参考,它不会抱怨


在我看来,上述代码是正确的。这是一个编译器错误,还是我做错了什么?

编译器是对的。转发引用(1)的工作方式是,如果传递类型为
U
的左值,它们将使用
U&
而不是
U
进行类型推断。因为在你的例子中,
v
是一个左值,
T
被推断为
v&
V&
是引用类型,它没有嵌套类型(甚至不能有嵌套类型)

处理转发引用时,必须始终使用
std::remove_reference
来获取底层类型:

template <class T>
typename std::remove_reference<T>::type::result_type
f (const W & w, T && t) {
    return 0;
}
模板
typename std::删除\引用::类型::结果\类型
f(康斯特水务、水务和水务){
返回0;
}


(1) 自2014年CppCon以来,“转发参考”被接受为“通用参考”的替代术语,因为它更好地抓住了意图。

非常感谢!我自己也不会想到这一点。我会尽快接受你的答复。