Using STL in Zorro strategies

Posted By: Zheka

Using STL in Zorro strategies - 09/22/20 12:50

I needed to use an unordered_map<> in my c++ code.

However, just adding #include <unordered_map> in the header file with the definition of the relevant class (even without using this container) generates numerous compiler errors.

Including it in pch.h kinda works, but shows a conflict around 'string'

C:\Users\Administrator\source\repos\EA_MasterPortfolioCPP\EA_Vars.h(147,1): error C2872: 'string': ambiguous symbol
1>C:\Zorro\include\trading.h(13,15): message : could be 'char *string'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\include\xstring(4564,7): message : or 'std::string'


Which concerns not only my script but the whole of Zorro framework which relies upon typedef char* string; in trading.h. (e.g. string sExchange, etc)

Wouldn't it be better to change this typedef to a different name (for c++)?

If not, what's the workaround?
Posted By: AndrewAMD

Re: Using STL in Zorro strategies - 09/22/20 15:10

Works for me.

The order of including headers is important. First, include your STL headers, then include the zorro header last.
Posted By: Zheka

Re: Using STL in Zorro strategies - 09/22/20 16:38

If I include <unordered_map> in ZorroDLL.cpp after pch.h and before zorro.h - all indeed compiles fine and there is no conflict with 'string'.

However, attempting to define an unordered_map <string, int> variable later shows that unordered_map is not recognized as a type by the compiler.

Where is the catch?
Posted By: AndrewAMD

Re: Using STL in Zorro strategies - 09/22/20 17:01

Again, this works for me, except I did not touch the precompiled headers. Try keeping that stuff out of the precompiled headers.

More importantly, it does not seem like a good idea to use a char pointer as a key in a map, as this can have some unexpected side effects.
Posted By: Zheka

Re: Using STL in Zorro strategies - 09/23/20 10:30

If the intention is to use an Asset as a key, what would you use and why?
Posted By: AndrewAMD

Re: Using STL in Zorro strategies - 09/23/20 12:49

Probably std::string. It has built-in content comparison operators. (In other words, it does strcmp() for you.)

Whereas if you use char*, you compare by pointer address value. The same string contents can exist at multiple addresses, so you can end up with duplicate entries for the same string content.

EDIT: Actually, you can use char* correctly in std::map, but you have to supply a comparison functor:
https://stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap/4157729#4157729

Also, you would still have to guarantee that the pointers are valid. At least with std::string, each string maintains its own copy of its full string. For that reason, you'd still be safer with std::string. (There are more optimized implementations available, but I will not discuss that here.)
Posted By: Zheka

Re: Using STL in Zorro strategies - 09/24/20 10:37

Clear, thank you. Will try.
© 2024 lite-C Forums