小编Gen*_*Bug的帖子

constexpr函数中的复合赋值:gcc与clang

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 …

c++ language-lawyer constexpr

17
推荐指数
2
解决办法
288
查看次数

使用 ref 限定符重载模板方法的解析

我正在使用特殊类型开发具有编译时访问功能的容器。我还想拥有一个使用数字的访问函数,以便为所有元素实现操作。因此我有这样的事情:

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 都接受它。谁是对的?

https://godbolt.org/z/4bmA2-

c++ visual-c++ language-lawyer

5
推荐指数
1
解决办法
272
查看次数

标签 统计

c++ ×2

language-lawyer ×2

constexpr ×1

visual-c++ ×1