sách gpt4 ăn đã đi

c++ - 为什么 gcc 不能为我的函数模板推断出正确的类型?

In lại Tác giả: Walker 123 更新时间:2023-11-30 04:06:05 25 4
mua khóa gpt4 giày nike

#include 
#include
template
struct id { typedef T type; };

template
void f(T b, typename id::type* a){}

int main() {
f(0, 0);
}

vs2013:好的!

g++4.8.2:compile error,such is the info:

main.cpp: In function 'int main()':
main.cpp:10:10: error: no matching function for call to 'f(int, int)'
f(0,0);
^
main.cpp:10:10: note: candidate is:
main.cpp:7:6: note: template void f(T, typename id::type*)
void f(T b, typename id::type* a){}
^
main.cpp:7:6: note: template argument deduction/substitution failed:
main.cpp:10:10: note: mismatched types 'typename id::type*' and 'int'
f(0,0);
^

câu trả lời hay nhất

原因是标准一直不清楚具有部分复合类型(例如指针星)的非推导上下文会发生什么,从而使参数不匹配但仍然可以通过隐式转换接受参数

câu hỏi http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1184确实通过添加注释来解决此问题,该注释说明类似于函数参数不包含推导的模板参数的情况,隐式转换也应该被允许以弥补不匹配。

从那时起,在模板参数的参数推导过程中发现了有关处理这些“隐式转换”的其他问题,并由 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1391 处理。 .

总的来说,我认为 1184 的效果是 GCC 应该接受你的代码,但由于在 #1391 中反射(reflect)的其他情况下的问题,他们可能会推迟实现 #1184,直到确定确切的细节。

关于c++ - 为什么 gcc 不能为我的函数模板推断出正确的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032032/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress