Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编译时缓冲解码_C++_Templates_Visual Studio 2017_C++17_Compile Time - Fatal编程技术网

C++ 编译时缓冲解码

C++ 编译时缓冲解码,c++,templates,visual-studio-2017,c++17,compile-time,C++,Templates,Visual Studio 2017,C++17,Compile Time,我有下面的类,我想在编译时将二进制输入解码为某个值。它在使用静态函数实现时工作得很好,但是我希望非静态函数在编译时工作,这有可能吗?任何帮助都将不胜感激 class BinaryDecoder { public: BinaryDecoder(const BinaryDecoder&) = delete; BinaryDecoder& operator=(const BinaryDecoder&) = delete; public: BinaryD

我有下面的类,我想在编译时将二进制输入解码为某个值。它在使用静态函数实现时工作得很好,但是我希望非静态函数在编译时工作,这有可能吗?任何帮助都将不胜感激

class BinaryDecoder
{
public:
    BinaryDecoder(const BinaryDecoder&) = delete;
    BinaryDecoder& operator=(const BinaryDecoder&) = delete;

public:
    BinaryDecoder(const std::uint8_t* buffer, size_t size)
      : m_buffer(buffer),
        m_size(size)
    {}

    template<typename T>
    constexpr bool decode(size_t offset, T& decodedValue) noexcept
    {
        if constexpr (std::is_same< std::remove_reference_t<T>, std::uint32_t>::value)
            return decodeBuffer(offset, decodedValue);

        return false;
    }

    template<typename T>
    static constexpr T decode(const std::uint8_t* buffer, const size_t size, const size_t offset) noexcept
    {
        if constexpr (std::is_same< std::remove_reference_t<T>, std::uint32_t>::value)
            return decodeBuffer(buffer, size, offset);

        return 0;
    }

private:
    static constexpr std::uint32_t decodeBuffer(
        const std::uint8_t* buffer,
        const size_t size,
        const size_t offset) noexcept
    {
        if (offset + sizeof(std::uint32_t) > size)
            return 0;

        const uint8_t b0 = buffer[offset], b1 = buffer[offset + 1], b2 = buffer[offset + 2], b3 = buffer[offset + 3];
        return (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
    }

    constexpr bool decodeBuffer(
        const size_t offset,
        std::uint32_t& decodedValue) noexcept
    {
        if (offset + sizeof(std::uint32_t) > m_size)
            return false;

        const uint8_t b0 = m_buffer[offset], b1 = m_buffer[offset + 1], b2 = m_buffer[offset + 2], b3 = m_buffer[offset + 3];
        decodedValue = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
        return true;
    }

private:
    const std::uint8_t* m_buffer;
    const size_t m_size;
};

int main()
{
    constexpr std::uint8_t input[] = { 0x84, 0x03, 0x00, 0x00, 'H', 'e', 'l', 'l', 'o', 0x00, 0x90 };       
    static_assert(900 == BinaryDecoder::decode<std::uint32_t>(input, sizeof(input), 0), "Decoded value not equal to 900");

    constexpr std::uint8_t input2[] = { 0x84, 0x33, 0x00, 0x00, 'H', 'e', 'l', 'l', 'o', 0x00, 0x90 };
    BinaryDecoder binaryDecoder(input2, sizeof(input2));
    constexpr std::uint32_t value = 900;
    binaryDecoder.decode(0, value);
    // Should not compile since it is not 900.
    static_assert(900 == value, "Decoded value not equal to 900");
    return 0;
}
我只是假设我的代码有问题,但Visual Studio对我来说很聪明。它不知何故在后台编译了它,并将其标记为错误。如果我将值更改为900,则所有编译都可以


Edit2:但是它没有在编译时解码,这正是我想要的。

没有constexpr构造函数?@Jarod42是的,你是对的,我已经解决了这个问题。是的,我应该有一个constexpr构造函数,但我相信问题是从解码函数返回值。我想我不能把它作为那样的引用?@Jarod42不,很抱歉,你是对的,用constexpr构造函数解决了这个问题。如果您将其作为答案提交,我将接受。作为替代方案,您可以返回一个
可选的
这对我有效:-您有什么问题?请说明预期用途和错误。
    constexpr std::uint32_t value = 0;
    binaryDecoder.decode(0, value);
    static_assert(900 == value, "Decoded value not equal to 900");