0

Here is MCVE of my problem:

#include <cstdlib>
#include <cstdint>

template<std::size_t CAPACITY>
class Buffer
{
public:
    template<std::size_t PARAM_SIZE>
    void pushArray(const uint8_t values[PARAM_SIZE])
    {
        // method's body (not important)
    }
};

template<std::size_t CAPACITY>
class BetterBuffer : public Buffer<CAPACITY>
{
public:
    template<typename TYPE>
    void push(TYPE value)
    {
        typedef uint8_t arrayType_t[sizeof(TYPE)];
        const arrayType_t &array = (arrayType_t&)value;
        Buffer<CAPACITY>::pushArray<sizeof(TYPE)>(array);
    }
};

int main()
{
    BetterBuffer<10> buffer;
    buffer.push(100);
    return 0;
}

The compiler prints the following error message.

main.cpp: In instantiation of ‘void BetterBuffer<CAPACITY>::push(TYPE) [with TYPE = int; long unsigned int CAPACITY = 10]’:
main.cpp:31:24:   required from here
main.cpp:24:40: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘long unsigned int’ to binary ‘operator<’
             Buffer<CAPACITY>::pushArray<sizeof(TYPE)>(array);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~

I compile it with command g++ -Wall -std=c++17 main.cpp.

I'm using g++ in version: 'g++ (Ubuntu 7.1.0-5ubuntu2~16.04) 7.1.0'.

If I remove the template parameter from class Buffer or move the method push from BetterBuffer to Buffer then program can compile with no problem.

What is wrong with this specific code?

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • 1
    You can use `Buffer:: template pushArray(array);` or `this->template pushArray(array);`. See https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords. – R Sahu Jun 16 '17 at 22:39
  • Thanks! I write in C++ quite a long time but I don't think that I saw this use of keyword `template` before. – Piotr Siupa Jun 16 '17 at 22:46
  • It's time to refresh your knowledge of C++ :) :) – R Sahu Jun 16 '17 at 22:48

0 Answers0