I am currently implementing my own version of a foward list and I wanted to be able to change between my class and STL's easily while compiling it. I've tried this method before and it worked... not sure if it was pure luck. Anyways, It isn't working now. Is there a better way for doing this?
#include "Header.h"
//selecting foward list
#ifdef MY_FOWARD_LIST
#include "Forward_List.h"
#define FWRDLIST Forward_list //name of the class that I've made
#else
#include <forward_list>
#define FWRDLIST std::forward_list
#endif // MY_FOWARD_LIST end
void CheckList(FWRDLIST<int> f_list){
// Testando iterators
std::cout << "\nSize: " << f_list.size() << std::endl;
if(f_list.empty()) std::cout << "Empty!\n";
else{
for(FWRDLIST<int>::iterator lit = f_list.begin(); lit != f_list.end(); lit ++){
std::cout << "Elements of the list: " << *lit << std::endl;
}
}
}
int main(){
std::cout <<">> Program start.\nBUILDING LIST...\n"
FWRDLIST<int> forward_list;
CheckList(forward_list);
}
I get the following error:
src/Drive_Foward_List.cpp: In function ‘int main()’:
src/Drive_Foward_List.cpp:7:18: error: expected ‘;’ before ‘Forward_list’
#define FWRDLIST Forward_list
src/Drive_Foward_List.cpp:31:3: note: in expansion of macro ‘FWRDLIST’
FWRDLIST<int> forward_list;
^
src/Drive_Foward_List.cpp:33:13: error: ‘forward_list’ was not declared in this scope
CheckList(forward_list);
Edit:
About using a constexpr: my assignment says that the user has to select wich list he would like to use through the command line at the terminal. So I guess it would be better to use a #define the way I have been using since I just need to -DUSE_MY_CLASS while at it... ? For what I've read, a constexpr variable does pretty much what a #define variable should be doing for my situation. Again, thank you very much.
> So I guess it would be better to use a #define the way I have been using
> since I just need to -DUSE_MY_CLASS while at it... ?
> For what I've read, a constexpr variable does pretty much what a #define variable should be doing for my situation.
A construct supported by the C++ type system and other language rules is inherently more robust and more flexible than a pure pre-processor technique.
For instance, if my_forward_list_for_trivial_types<T> is specially written for trivial types:
> A construct supported by the C++ type system and other language rules is inherently more robust and more flexible than a pure pre-processor technique.
I'll keep this on mind for my future assignments . I'm using constexp a lot more now. Thanks for helping me out!