Jump to content
ViVo

std::vector as template argument

Recommended Posts

 

i wrote very simple template module (select_unit.h) with vector as one of its 3rd argument.

i encounter difficulty instantiating it in the upper "floor" (up_mod.h).

the marked red code should be the definition of std::vector of unsigned integers (template's 3rd parameter in select_unit module). the compiler complains for const 3rd argument..., how should i define the vector?

any help would be appreciated.

V.

 

select_unit.h

template <unsigned w_bus,unsigned w_sel, std::vector<unsigned> &sel_bits>
SC_MODULE(select_unit) {

    /* -----------Input Ports---------------------------------------------------------  */
    sc_in            <sc_uint<w_bus>>            in                                         ;

    /* -----------Output Ports--------------------------------------------------------  */
    sc_out            <sc_uint<w_sel>>            out                                        ;
    
    /* -----------Input/Output Ports--------------------------------------------------  */

    /* -----------Local Signal Declarations-------------------------------------------  */

    /* -----------Internal modules / variables----------------------------------------  */

    /* -----------Methods-------------------------------------------------------------  */
    void            thread0(void)                                                        ;

    /* -----------Constructor---------------------------------------------------------  */
    SC_CTOR(select_unit) : in("in"), out("out")
    { ...

    }

}                                                                                        ;


template <unsigned w_bus,unsigned w_sel,std::vector<unsigned> &sel_bits>
void select_unit<w_bus, w_sel, sel_bits>::thread0(void)
{  ...
}
 

up_mod.h


SC_MODULE(up_mod) {

    /* -----------Input Ports---------------------------------------------------------  */
    /* -----------Output Ports--------------------------------------------------------  */
    /* -----------Input/Output Ports--------------------------------------------------  */

    /* -----------Local Signal Declarations-------------------------------------------  */
    /* -----------Internal modules/variables------------------------------------------  */
    ...

    std::vector<unsigned> bits2mnt;
    select_unit<U0_WIDTH, 18, bits2mnt>                    *select0                        ;

    /* -----------Constructor----------------------------------------------- ---------- */
    SC_CTOR(up_mod)
    {
          ...

        /* -----------Initialize Sub Modules------------------------------------------  */
        select0        = new select_unit<U0_WIDTH, 18, bits2mnt>                           ;
}

}

Share this post


Link to post
Share on other sites

Templates are compile time feature in C++. So you can't use values that are not compile-time constants as non-type template parameters.

Instead use a constructor parameter to pass arguments:

SC_HAS_PROCESS(select_unit);

select_unit(::sc_core::sc_module_name name, std::vector<unsigned> &sbits) : sel_bits(sbits) { /*...*/ }

std::vector<unsigned> &sel_bits;

 

Share this post


Link to post
Share on other sites

Hello Roman,

thank you very much for your help.. i revised my code style upon your inputs..

As 1st step, i'm converting to the "recommended" sytle tammed in the "SystemC from the ground up" book.    As shown in the attached files...

The problem is that i get lots of errors saying: "template is illigal". 

Can i use this code style with parameterized module-type?

BR,

V

basic_process_ex.cpp

basic_process_ex.h

main.cpp

Share this post


Link to post
Share on other sites
On 11/5/2017 at 5:54 AM, ViVo said:

Hello Roman,

As 1st step, i'm converting to the "recommended" sytle tammed in the "SystemC from the ground up" book.    As shown in the attached files...

The problem is that i get lots of errors saying: "template is illigal". 

Your code compiles w/o any errors on my machine with g++. 

SC_HAS_PROCESS macro is not required, since you have used SC_CTOR.  I recommend to look how SC_MODULE, SC_CTOR, SC_HAS_PROCESS and SC_THREAD are expanded to understand what each one is doing.

Share this post


Link to post
Share on other sites
19 hours ago, ViVo said:

So, windows is not functioning well ?

i'm searching for stable environment for systemc, windows isn't sufficient ? what should i do ?

V

On Windows both Visual Studio and MinGW work well with SystemC. 

Since your question is not about SystemC, but rather about C++ language, I recommend to extract minimal code sample and ask in C++  community. For example on https://stackoverflow.com/  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×