求助重载运算符+模板特例化=>CE
  • 板块学术版
  • 楼主_caiji_
  • 当前回复10
  • 已保存回复10
  • 发布时间2021/7/25 22:10
  • 上次更新2023/11/4 13:19:32
查看原帖
求助重载运算符+模板特例化=>CE
390033
_caiji_楼主2021/7/25 22:10
#include <string>
#include <iostream>
using namespace std;
struct A{
    template<class T> string operator()(T x){
        return "No";
    }
    template</*int*/> string operator()(int x){
        return "Yes";
    }
};
A check;
int main(){
    cout<<check("1")<<endl;
    cout<<check(21)<<endl;
    return 0;
}

就这个代码,会 CE,这是编译信息:

/tmp/compiler_a4oh_y2p/src:8:21: 错误:显式特例化出现在非命名空间作用域‘struct A’中
     template</*int*/> string operator()(int x){
                     ^
                     

看了 cppreference.com 了,也 BDFS 了,还是搞不懂,一直 CE,请问应该怎么改才不会 CE?

2021/7/25 22:10
加载中...