加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 模板错误

发布时间:2020-12-16 09:29:21 所属栏目:百科 来源:网络整理
导读:我听说C模板在使用之前不会产生错误.这是真的吗?有人可以解释我们是如何工作的吗? 解决方法 模板遵循两阶段编译模型. struct X{private: void f(){}};templateclass T void f(T t){ int; // gives error in phase 1 (even if f(x) call is commented in ma
我听说C模板在使用之前不会产生错误.这是真的吗?有人可以解释我们是如何工作的吗?

解决方法

模板遵循两阶段编译模型.

struct X{
private:
   void f(){}
};

template<class T> void f(T t){
   int;   // gives error in phase 1 (even if f(x) call is commented in main)
   t.f(); // gives error only when instantiated with T = X,as x.f() is private,in phase 2
}

int main(){
   X x;
   f(x);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读