大佬教程收集整理的这篇文章主要介绍了c – 实例化和名称绑定点,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <iostream> void f(int){std::cout<<"int"<<std::endl;}//3 template <typename T> void g(T t) { f(t);//4 } void f(doublE){std::cout<<"double"<<std::endl;} int main() { g<int>(1);//1.point of instantiation for g<int> g<double>(1.1);//2.point of instantiation for g<double>,so f(doublE) is visible from here? return 0; }
我虽然f是一个从属名称,但是1是g<的实例化点. int>和2.是g< 2的实例化点. double>,所以对于g(1.1),f(doublE)是可见的,但输出是
int int
如果我在3处评论f(int)的声明,gcc报告错误(不是惊讶),并指出f(t)在4是实例化点(惊讶!!).
test.cpp: In instantiation of ‘void g(T) [with T = int]’: test.cpp:16:10: required from here test.cpp:9:5: error: ‘f’ was not declared in this scope,and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] f(t); ^
任何人都可以为我清除实例化和名称绑定的概念吗?
ADL无法找到f(doublE),因为内置类型没有关联的类或名称空间.如果传入类类型的参数,并且f类型的重载采用此类型,则ADL将能够找到它.例如:
void f(int); template <typename T> void g(T t) { f(t); } class A {}; void f(doublE); void f(A); int main() { g(1); // calls f(int) g(1.1); // calls f(int) g(A{}); // calls f(A) }
以上是大佬教程为你收集整理的c – 实例化和名称绑定点全部内容,希望文章能够帮你解决c – 实例化和名称绑定点所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。