4 ms·
You'd need a second #define, something like: #define ARRAY_ELEMENT_NAME int_ptr #define ARRAY_ELEMENT_TYPE int* #include "array_template_impl.h" A
by 1wd 10y ago
You'd need a second #define, something like:
#define ARRAY_ELEMENT_NAME int_ptr
#define ARRAY_ELEMENT_TYPE int*
#include "array_template_impl.h"
And then use ARRAY_ELEMENT_NAME only in the definitions of ARRAY and ARRAY_FUNC, and use ARRAY_ELEMENT_TYPE instead everywhere else.
- tines 10y agoActually now that I think about it, it might be better to just make a typedef and then you can go back to using the same name everywhere. I think that would solve the problem. This technique seems to take care of the bulk of the use of templates in C++, i.e. simple data structure or function definitions. But it's not a full replacement, because I don't think you can use this to do things like pass an integer to one of these templates, then have that template use another template and pass a calculation based on that number to the other template like you could do in C++. Something like this: template<int X> struct foo_t { /* ... */ }; template<int Y> struct bar_t { foo_t<Y / 2> foo; }; so that bar_t<50> and bar_t<24> are different types which contain a foo_t<25> and foo_t<12> respectively. But it's cool nevertheless.
- 1wd 10y agoAh yes, using a typedef would work too, nice. It for sure not quite the same as C++ templates, but if you can tolerate crazy things you can do a lot with the preprocessor. See http://www.boost.org/libs/preprocessor/ http://www.boost.org/libs/preprocessor/ (supports both C++ and C). If you have foo_template.h: #define FOO_T CONCAT(CONCAT(foo_,X),_t) struct FOO_T { /* ... */ }; and bar_template.h: #define X Y/2 #include "foo_template.h" #define BAT_T CONCAT(CONCAT(bar_,Y),_t) struct BAR_T { FOO_T foo; }; It might almost work, but you'd need to pull out some more tricks I'm sure. Maybe BOOST_PP_DIV(Y,2) would help. In practice I'd prefer something sane. :)