accellera-official / systemc

SystemC Reference Implementation
https://systemc.org/overview/systemc/
Apache License 2.0
445 stars 141 forks source link

Compilation error with sc_fifo< std::vector<...> > #76

Closed alguryanow closed 2 months ago

alguryanow commented 4 months ago

I want to store std::vector in sc_fifo. But by definining a variable of type

sc_fifo< std::vector<uint16_t> > fifo;

I get the following error:

In file included from /home/gav/SystemC-2.3.4/include/systemc:84,
                 from /home/gav/systemc-test/src/main.cpp:1:
/home/gav/SystemC-2.3.4/include/sysc/communication/sc_fifo.h: In instantiation of ‘void sc_core::sc_fifo<IF>::print(std::ostream&) const [with T = std::vector<short unsigned int>; std::ostream = std::basic_ostream<char>]’:
/home/gav/SystemC-2.3.4/include/sysc/communication/sc_fifo.h:431:12:   required from ‘std::ostream& sc_core::operator<<(std::ostream&, const sc_core::sc_fifo<T>&) [with T = std::vector<short unsigned int>; std::ostream = std::basic_ostream<char>]’
/home/gav/systemc-test/src/main.cpp:28:18:   required from here
/home/gav/SystemC-2.3.4/include/sysc/communication/sc_fifo.h:317:16: error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘std::vector<short unsigned int>’)
  317 |             os << m_buf[i] << ::std::endl;
      |             ~~~^~~~~~~~~

But actually I have defined operator<< for std::vector<uint16_t>.

Here is the full code of my test program:

#include <systemc>
using namespace sc_core;

#include <vector>
#include <stdint.h>

std::ostream& operator<<(std::ostream& out, const std::vector<uint16_t>& data )
{
    return out;
}

int sc_main(int argc, char* argv[])
{
    sc_fifo< std::vector<uint16_t> > tmp;
    std::cout << tmp;

    std::vector<uint16_t> ddd;          // just for checking operator<< for std::vector<uint16_t> is really defined
    std::cout << ddd << std::endl;

    sc_start(4, SC_SEC);

    return 0;

};

If I comment lines

    sc_fifo< std::vector<uint16_t> > tmp;
    std::cout << tmp;

the compilation finished successfully, but when I uncommnent them then I get the error as provided above.

My OS is Ubuntu 22.04. Tested on SystemC v2.3.3 and v2.3.4

lmailletcontoz commented 2 months ago

Your issue seems not SystemC-related, but most likely a C++ issue (operator visibility when instantiating the template). Please post on a C++ forum for further discussion.

dcblack commented 2 months ago

It is not a problem with SystemC, but rather understanding that sc_signal and sc_fifo have requirements on T that are not part of standard C++ containers. A simple wrapper can solve this. You should probably ask for this type of help on the Accellera SystemC forum

See my GitHub for an example of how to do this.

You probably need more training in Modern C++ and SystemC to avoid this in the future.

Note: I teach C++ and SystemC for Doulos. Our graduates know this stuff. See Doulos for more information.