C++ 2-phase lookup

This 2-phase look up of g++ (gnu C++) seems not inconsistency: builtin types are not treat equivalent with user defined type.

#include <stdio.h>

class A {}; void f(A) { printf("%s\n", __PRETTY_FUNCTION__); }
class B {};

// now g(T) knew A,B,int,...
// phase 1 lookup just success for f(A)
template<class T> void g(T x) { f(x); }

// after definition of g(T) and class B
void f(B) { printf("%s\n", __PRETTY_FUNCTION__); }

void f(int x) { printf("%s\n", __PRETTY_FUNCTION__); }
void f(int*x) { printf("%s\n", __PRETTY_FUNCTION__); }

int main(int argc, char** argv) {
    g(A());   // should always(phase 1 and phase 2) be OK
    g(B());   // f(B) definition is after g(T) definition, phase 1 just got f(A)
    g(B());   //      but phase 2 got f(B)
    g(100);   // intend to let g calling f(int), but failed in g++ 4.1 or newer
    g(&argc); // intention: similar as g(100)
    return 0;
}

原文链接: https://www.cnblogs.com/rockeet/archive/2011/11/11/3666881.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 2-phase lookup

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/36215

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午12:58
下一篇 2023年2月8日 下午12:59

相关推荐