![](/img/trans.png)
[英]Different results between clang/gcc and MSVC for templated constructor in base class
[英]Failure to select correct operator== with MSVC but not gcc/clang for templated class
以下示例使用 gcc 和 clang 编译良好,但无法在 MSVC 中编译。 我想知道我是否无意中跌入了非标准领域? 如果不是,哪个编译器是正确的? 是否有解决方法? 最小示例 ( https://godbolt.org/z/PG35hPGMW ):
#include <iostream>
#include <type_traits>
template <class T>
struct Base {
Base() = default;
Base(T) {}
static constexpr bool isBase = true;
};
template <class U>
constexpr std::enable_if_t<U::isBase, bool> EnableComparisonWithValue(U const *) {
return false;
}
template <class>
constexpr bool EnableComparisonWithValue(...) {
return true;
}
template <class T, class U>
bool operator==(Base<T> const &, Base<U> const &) {
std::cout << "operator==(Base, Base)" << std::endl;
return true;
}
template <class T, class U,
std::enable_if_t<EnableComparisonWithValue<U>(nullptr), int> = 0>
bool operator==(Base<T> const &, U const &) {
std::cout << "operator==(Base, U)" << std::endl;
return true;
}
template <class U, class T,
std::enable_if_t<EnableComparisonWithValue<U>(nullptr), int> = 0>
bool operator==(U const &, Base<T> const &) {
std::cout << "operator==(U, Base)" << std::endl;
return true;
}
int main() {
Base<int> b1, b2;
b1 == 42; // gcc and clang compile, msvc does not
}
MSVC 抛出编译错误C2676: binary '==': 'Base<int>' does not define this operator or a conversion to a type acceptable to the predefined operator
。 clang 和 gcc 按预期调用operator==(Base, U)
。 有趣的是,如果我删除Base
中的所有成员并将其定义为template <class T> struct Base{};
.
背景:我有另一个 class template <class T> Derived: Base<T>
不包含附加数据。 我想重用所有operator==
而不必为Derived
重新定义它们。 如果没有 SFINEA 的东西,将Derived<int>
与int
进行比较会导致模糊的运算符调用,因为底部的两个operator==
定义将U
推断为Derived<int>
(AFAIK 正确)。 所以我的想法是禁用它们以强制编译器使用operator==(Base<T> const &, Base<U> const &)
。 但后来我遇到了上述问题。
此外,除了为Base
和Derived
的所有组合定义运算符之外,是否还有解决方法?
我很惊讶 MSVC 没有编译你的代码,这在我看来是完全正确的。
所以...不确定...但我想这是一个 MSVC 错误。
无论如何...考虑到您还要求解决方法...我看到如果您启用/禁用运算符的返回类型,MSVC 也适用于 SFINAE,所以我建议您按如下方式重写运算符
template <class T, class U>
std::enable_if_t<EnableComparisonWithValue<U>(nullptr), bool> operator==(Base<T> const &, U const &) {
std::cout << "operator==(Base, U)" << std::endl;
return true;
}
template <class U, class T>
std::enable_if_t<EnableComparisonWithValue<U>(nullptr), bool> operator==(U const &, Base<T> const &) {
std::cout << "operator==(U, Base)" << std::endl;
return true;
}
以下是一个完整的编译示例,其中还有一个Derived
class
#include <iostream>
#include <type_traits>
template <class T>
struct Base {
Base() = default;
Base(T) {}
static constexpr bool isBase = true;
};
struct Derived : public Base<int>
{ };
template <class U>
constexpr std::enable_if_t<U::isBase, bool> EnableComparisonWithValue(U const *) {
return false;
}
template <class>
constexpr bool EnableComparisonWithValue(...) {
return true;
}
template <class T, class U>
bool operator==(Base<T> const &, Base<U> const &) {
std::cout << "operator==(Base, Base)" << std::endl;
return true;
}
template <class T, class U>
std::enable_if_t<EnableComparisonWithValue<U>(nullptr), bool> operator==(Base<T> const &, U const &) {
std::cout << "operator==(Base, U)" << std::endl;
return true;
}
template <class U, class T>
std::enable_if_t<EnableComparisonWithValue<U>(nullptr), bool> operator==(U const &, Base<T> const &) {
std::cout << "operator==(U, Base)" << std::endl;
return true;
}
int main() {
Base<int> b1, b2;
Derived d1, d2;
b1 == b2; // Compiles fine
b1 == 42; // gcc and clang compile, msvc does not
d1 == d2;
d1 == b1;
b2 == d2;
d1 == 42;
42 == d2;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.