template<class A, class B> constexpr int f(A a, B b) {
a /= b;
return a;
}
constexpr int x = f(2, 2); // a, b: int
constexpr int y = f(2., 2.); // a, b: double
constexpr int z = f(2, 2.); // a: int, b: double //<-- BOOM!
constexpr int w = f(2., 2); // a: double, b: int
int main() {}
Run Code Online (Sandbox Code Playgroud)
代码不在clang中编译,它产生以下诊断:
error: constexpr variable 'z' must be initialized by a constant expression
Run Code Online (Sandbox Code Playgroud)
MSVC坠毁(根据godbolt)并且gcc工作正常.如果a …
我正在使用特殊类型开发具有编译时访问功能的容器。我还想拥有一个使用数字的访问函数,以便为所有元素实现操作。因此我有这样的事情:
struct S
{
template<int I> int& f();
template<class Q> int& f();
};
Run Code Online (Sandbox Code Playgroud)
我想禁止访问临时对象,所以我为类型访问添加了一个重载:
struct S
{
template<int I> int& f();
template<class Q> int& f() &;
template<class Q> int& f() && = delete;
};
Run Code Online (Sandbox Code Playgroud)
但是后来我遇到了 msvc 编译器的问题:
<source>(4): error C2560: 'int &Test::f(void) &': cannot overload a member function with ref-qualifier with a member function without ref-qualifier
Run Code Online (Sandbox Code Playgroud)
但是 gcc 和 clang 都接受它。谁是对的?