enchanter Posted April 22, 2014 Report Posted April 22, 2014 I have a top level module with template like this: template <int TOP_DEPTH=10> SC_MODULE(my_top){} And I have several sub modules like this: template <int DEPTH=10> SC_MODULE(sub_module_xxx> I try to instantiate sub modules with the modified DEPTH from top level: const int DEPTH_MODULE_A = <math1 on TOP_DEPTH> const int DEPTH_MODULE_B = <math2 on TOP_DEPTH> ... sub_module_a<DEPTH_MODULE_A> *pSubModuleA; sub_module_b<DEPTH_MODULE_B> *pSubModuleB; ... I got compile error about "invalid use of non-static data member". I am not sure what's the right way to do it in C++. (I don't want DEPTH_MODULE_xxx to be static, because my top_module may be used as submodule in other project with different value of TOP_DEPTH. ) Quote
dakupoto Posted April 22, 2014 Report Posted April 22, 2014 I have a top level module with template like this: template <int TOP_DEPTH=10> SC_MODULE(my_top){} And I have several sub modules like this: template <int DEPTH=10> SC_MODULE(sub_module_xxx> I try to instantiate sub modules with the modified DEPTH from top level: const int DEPTH_MODULE_A = <math1 on TOP_DEPTH> const int DEPTH_MODULE_B = <math2 on TOP_DEPTH> ... sub_module_a<DEPTH_MODULE_A> *pSubModuleA; sub_module_b<DEPTH_MODULE_B> *pSubModuleB; ... I got compile error about "invalid use of non-static data member". I am not sure what's the right way to do it in C++. (I don't want DEPTH_MODULE_xxx to be static, because my top_module may be used as submodule in other project with different value of TOP_DEPTH. ) Hello Sir, Please look up some good reference on C++ BEFORE working with SystemC. Static members of class may be initilaized in the constructor initialization list. Also, a class with a modified template argument is not a sub-class of a given super-class. Please familiarize yourself with all the core features of C++ before venturing into SystemC. Hope that helps. Quote
Philipp A Hartmann Posted April 22, 2014 Report Posted April 22, 2014 I am not sure what's the right way to do it in C++. (I don't want DEPTH_MODULE_xxx to be static, because my top_module may be used as submodule in other project with different value of TOP_DEPTH. ) I don't understand, why using the top module with different values of TOP_DEPTH is a showstopper for declaring the DEPTH_MODULE_xxx as static members of my_top. Integer template arguments have to be known at compile-time in order to instantiate the correct type. Would something like the following be sufficient for your situation? template <int DEPTH=10> SC_MODULE(sub_module_xxx); template <int TOP_DEPTH=10> SC_MODULE(my_top) { static const int depth = TOP_DEPTH; // I usually forward template constants to proper member constants static const int depth_a = depth + 1; // or other math on depth static const int depth_b = depth / 2; // ... sub_module_a<depth_a> sub_a; // no need for pointers? sub_module_b<depth_b> sub_b; SC_CTOR(my_top) : sub_a("sub_a") // initialize sub-modules , sub_b("sub_b") {} }; int sc_main(int, char*[]) { my_top<17> t1("t1"); my_top<42> t2("t2"); sc_assert( t1.depth == 17 && t1.depth_a == 18 ); sc_assert( t2.depth == 42 && t2.depth_b == 21 ); return 0; } Greetings from Oldenburg, Philipp enchanter 1 Quote
enchanter Posted April 23, 2014 Author Report Posted April 23, 2014 Hi Philipp: Thanks for your reply. The solution works perfect for me. But from my very basic C++ knowledge, I think 'static' means share the variable with all the instances of the class. So that means the 't1' and 't2' should share 'depth', 'depth_a', and 'depth_b'. And the 'const' means no one can change it. Then I am confused that how could 't1' and 't2' get different value of 'depth' and 'depth_x'? Quote
dakupoto Posted April 23, 2014 Report Posted April 23, 2014 Hi Philipp: Thanks for your reply. The solution works perfect for me. But from my very basic C++ knowledge, I think 'static' means share the variable with all the instances of the class. So that means the 't1' and 't2' should share 'depth', 'depth_a', and 'depth_b'. And the 'const' means no one can change it. Then I am confused that how could 't1' and 't2' get different value of 'depth' and 'depth_x'? Hello Sir, I am afraid you should look up a good reference on C++. A public static member of a class can be used by all instances(objects), so that even if an instance of its containing class is NOT created, the static member to be used. Please check a good C++ reference -- good things do not come easy. Hope that helps. Quote
Philipp A Hartmann Posted April 23, 2014 Report Posted April 23, 2014 But from my very basic C++ knowledge, I think 'static' means share the variable with all the instances of the class. So that means the 't1' and 't2' should share 'depth', 'depth_a', and 'depth_b'. And the 'const' means no one can change it. Then I am confused that how could 't1' and 't2' get different value of 'depth' and 'depth_x'? Yes, static members are shared among all instances of a class. But, a class template is more like a recipe to build classes. The instances of a class template are all different classes. The static members of "t1" and "t2" can differ, because t1 and t2 are instances of different classes (my_top<17> and my_top<42>). These classes just happen to both have static constants named depth, etc. as they are cooked (instantiated) from the same recipe (template). /Philipp Quote
enchanter Posted April 24, 2014 Author Report Posted April 24, 2014 Hi Philipp: Thanks for your clear explanation. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.