tcsullivan / consteval-huffman

Compile-time Huffman coding compression using C++20
The Unlicense
165 stars 10 forks source link

data() method wrong return type #7

Open uchenily opened 1 month ago

uchenily commented 1 month ago

The data() method seems to get a wrong return type:

    auto data() const noexcept {
        if constexpr (bytes_saved() > 0)
            return compressed_data; // unsigned char[]
        else
            return raw_data; // huffman_string_container
    }

Should return raw_data here be changed to return raw_data.data?

uchenily commented 1 month ago

I notice that this class has implemented operator const T* method, but I think we still need to explicit cast to const char * here(return (const char *)raw_data), otherwise, the compiler will report an error:

test_huffman.cpp:438:35: error: call to consteval function ‘data.huffman_compressor<detail::huffman_string_container<char, 48>{"A long string to be compressed at compiled time"}>::data().detail::huffman_string_container<char, 48>::operator const char()’ is not a constant expression 438 | std::cout << std::string_view{(const char )data.data(), data.size()} << '\n'; | ^~~~~~~~~ test_huffman.cpp:438:58: error: call to non-‘constexpr’ function ‘auto huffman_compressor::data() const [with auto raw_data = detail::huffman_string_container<char, 48>{"A long string to be compressed at compiled time"}]’ 438 | std::cout << std::string_view{(const char *)data.data(), data.size()} << '\n'; | ~~~~~^~ test_huffman.cpp:371:10: note: ‘auto huffman_compressor::data() const [with auto raw_data = detail::huffman_string_container<char, 48>{"A long string to be compressed at compiled time"}]’ declared here 371 | auto data() const noexcept -> auto {

tcsullivan commented 1 month ago

I see, good catch. I would make the change from return raw_data to return raw_data.data. It might also make sense to just get rid of huffman_string_container's user-defined conversion and replace it with an operator[]; that's all the compressor code really needs.