实例化函数模板时出错

Error instantiating function template

本文关键字:出错 函数模板 实例化      更新时间:2023-10-16

我正在尝试下面简单的事情来练习ADL的行为

 namespace test{
   struct S{ public: s():a(10){} int a;};
   template<typename T>
   void fun(S o){
       T a{};
       std::cout<<(a+o.a)<<"n";
   }
 }
 int main(){
     test::S  A;
     fun<int>(A);
 }
    error1: expected primary-expression before ‘int’
       fun<(int)>(A);
    error2:'fun’ was not declared in this scope
       fun<int>(A);

我不确定第一个错误(有任何东西来处理c++令人烦恼的解析吗?)和第二个错误,因为我已经通过了S类型的参数,我相信ADL会考虑S及其相关的namespace test,但不确定为什么我得到'fun’ was not declared错误。

您在test-namespace中放置了fun<>

用test::fun