c++ - 我可以忽略 "Intellisense: (E0028) expression must have a constant value"吗?

标签 c++ compiler-errors intellisense constexpr

我正在创建一个位掩码类来处理超过 64 位的数据。我想在编译时生成掩码。下面编译并运行良好,但在使用 Ctor 设置单个位时在 VS 2017 中吐出错误消息。在 Ming-w64 中运行时没有错误消息。我可以安全地忽略 VS 吗?

#include <type_traits>
#include <array>


// N is number of uint64's required to hold bits, code below simplified for up to 128 bits
template<int N>
class CoreBitmaskBase
{
public:
    std::array<uint64_t, N> mBitsets_;

    constexpr CoreBitmaskBase() noexcept
        : mBitsets_()
    {}

    // Ctor for setting a single bit
    constexpr CoreBitmaskBase(const uint64_t& Bit) noexcept
        : mBitsets_(InitSetBit(Bit))
    {}

    constexpr std::array<uint64_t, N> InitSetBit(const uint64_t& Bit);
};

template<int N>
constexpr std::array<uint64_t, N> CoreBitmaskBase<N>::InitSetBit(const uint64_t& Bit)
{
    return std::array<uint64_t, N>({ (uint64_t)1 << Bit });
}

// if setting a bit index > 63, adjust mBitsets_'s second element
template<>
constexpr std::array<uint64_t, 2> CoreBitmaskBase<2>::InitSetBit(const uint64_t& Bit)
{
    if (Bit < 64) return std::array<uint64_t, 2>({ (uint64_t)1 << Bit, 0 });
    else return std::array<uint64_t, 2>({ 0, (uint64_t)1 << (Bit % 64) });
}

// B is number of bits we need for a mask
template<int B, typename Enable = void>
class CoreBitmask : public CoreBitmaskBase<1>
{
    using BaseType = CoreBitmaskBase<1>; 

public:
    constexpr CoreBitmask() noexcept
    {
    }

    constexpr CoreBitmask(const uint64_t& SetBit) noexcept
        : BaseType(SetBit)
    {
    }
};

// specialization for bitmask larger than 64 bits
template<int B>
class CoreBitmask<B, typename std::enable_if<(B > 64)>::type> : public CoreBitmaskBase<2>
{
    using BaseType = CoreBitmaskBase<2>;

public:
    constexpr CoreBitmask() noexcept
    {
    }

    constexpr CoreBitmask(const uint64_t& SetBit) noexcept
        : BaseType(SetBit)
    {
    }
};

主要.cpp

#include "CoreBitmask.h"

CoreBitmask<128> Mask1(67); // no warning

constexpr CoreBitmask<128> Mask2(67);
// E0028 expression must have a constant value
// access to uninitialized subobject(member "std::array<_Ty, _Size>::_Elems [with _Ty=uint64_t, _Size=2U]")

最佳答案

我找到了一个解决方案。将 std::move 添加到有问题的 ctor 时,智能感知警告消失,如下所示:

// Ctor for setting a single bit
constexpr CoreBitmaskBase(const uint64_t& Bit) noexcept
    : mBitsets_(std::move(InitSetBit(Bit)))
{}

关于c++ - 我可以忽略 "Intellisense: (E0028) expression must have a constant value"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131050/

相关文章:

c++ - 如何同步EXE/DLL文件的SVN版本和版本资源?

c++ - std::atomic::fetch_add 是 x86-64 上的序列化操作吗?

java - 由于重复类中潜在的多Dex问题导致缺少gms.maps.GoogleMap

Android 出现 eclipse proguard.cfg 问题

html - Visual Studio 2013 - 具有多个类的 HTML 元素和与智能感知的斗争

ReSharper - 如何在 IntelliSense 中显示自定义代码片段

c++ - 仅用于代理目的的组合对象与哪种设计模式相关?

c++ - 如何完全遍历 QStandardItemModel?

perl - 如果哈希键未在初始哈希定义中定义,有没有办法使 perl 编译失败?

xml - Visual Studio 2010 中的 XSD key/keyref intellisense 验证支持