我有一个问题,我归结为以下问题,==即使操作符使用编译失败(C++17,在 GCC 5.x、8.x 和 9.x 上测试):
template <int N> struct thing {
operator const char * () const { return nullptr; }
bool operator == (const thing<N> &) const { return false; }
};
int main () {
thing<0> a;
thing<1> b;
a == b; // i don't want this to compile, but it does
}
Run Code Online (Sandbox Code Playgroud)
它编译的原因是因为编译器选择这样做:
(const char *)a == (const char *)b;
Run Code Online (Sandbox Code Playgroud)
取而代之的是:
// bool thing<0>::operator == (const thing<0> &) const
a.operator == (b);
Run Code Online (Sandbox Code Playgroud)
因为后者不是匹配项,因为b …
c++ templates operator-overloading implicit-conversion c++17