初始化 C 数组的引用成员不编译 visual studio 2015

Initialise a reference member to C array don't compile on visual studio 2015

当我尝试在 g++ 上编译下面的代码时它可以工作,但在 vs2015 上它失败并显示消息: 错误 C2440:'initializing':无法从 'const bool *' 转换为 'const bool (&)[3]'

#include <iostream>

enum class Direction
{
       RIGTH,
       LEFT
};

struct Buffer
{
       int catRigth = 4;
       int catLeft = 8;
       bool dogRigth[3] = {true, false, true};
       bool dogLeft[3] = {false, true, false};
};

struct Bind
{
       const int &cat;
       const bool (&dog)[3];
       Bind(const Buffer &buf, Direction direction) :
              cat(direction == Direction::RIGTH ? buf.catRigth : buf.catLeft),
              dog(direction == Direction::RIGTH ? buf.dogRigth : buf.dogLeft)
       {
       }
};

int main(int argc, char* argv[])
{

       const Buffer buff;

       Bind bindRigth(buff, Direction::RIGTH);
       Bind bindLeft(buff, Direction::LEFT);

       int catRigth = bindRigth.cat;
       int catLeft = bindLeft.cat;

       std::cout << catRigth << " " << catLeft;
}

它是标准的 C++ 代码还是 gcc 特定的行为?

MSVC 不应该将其类型衰减为 const bool *:

5.16.4 If the second and third operands are glvalues of the same value category and have the same type, the result is of that type and value category and it is a bit-field if the second or the third operand is a bit-field, or if both are bit-fields.

MSVC 的解决方法可能是:

#include <utility>

const struct A {
    bool a[3] = {false};
} obj;

template <class Lhs, class Rhs>
auto &&Conditional(const bool x, Lhs &&lhs, Rhs &&rhs) {
    if (x)
        return std::forward<Lhs>(lhs);
    return std::forward<Rhs>(rhs);
}

int main(int argc, char* argv[]) {
    const bool (&t)[3] = Conditional(true, obj.a, obj.a);
    return 0;
}

PS: Conditional 不是 constexpr 函数。

或者:const bool (&t)[3] = *(true ? &obj.a : &obj.a);