Unable to initialize static set of strings inside templatized class in CLang, works fine in GCC - string

I'm using Boost config_file_iterator to parse a parameters file for different modules in the standard Boost way:
[Module 1 name]
M1_Key1=value1
[Module 2 name]
M2_Key1=value2
For that, Boost has the nice feature to accept a std::set containing all the valid keys, to throw an exception in case a non-valid one is passed.
boost::program_options::detail::config_file_iterator cfgIt(configStream, ALLOWED_PARAMS);
In my code, I've a set of modules, each one with its own set of parameters, In each one of the module header I have (as non-member variables):
module1.h:
static const std::string MODULE1_NAME = "Module 1 name";
static const std::string MODULE1_KEY1 = "M1_Key1";
module2.h:
static const std::string MODULE2_NAME = "Module 2 name";
static const std::string MODULE2_KEY1 = "M2_Key1";
In the header file for the parameters parser:
class ParametersParser
{
static const std::set<std::string> ALLOWED_PARAMS;
}
Finally in the source file of that parameter parser groups all the allowed values for the config iterator from the different modules:
#include "module1.h"
#include "module2.h"
const std::set<std::string> ParametersParser::ALLOWED_PARAMS =
{{
MODULE1_NAME + std::string(".") + MODULE1_KEY1,
MODULE2_NAME + std::string(".") + MODULE2_KEY1
}};
since Boost expects that as the allowed parameters set, a set of string of the way "Module X Name.MX_KeyY", and that worked fine.
However, because of other reasons, I've templatized the class that parses the file, so now my set of strings is declared also in the same header file, as:
#include "module1.h"
#include "module2.h"
/* Class definition */
template <class T>
class ParametersParser
{
static const std::set<std::string> ALLOWED_PARAMS;
};
/* Static member initialization */
template <class T>
const std::set<std::string> ParametersParser<T>::ALLOWED_PARAMS =
{{
MODULE1_NAME + std::string(".") + MODULE1_KEY1,
MODULE2_NAME + std::string(".") + MODULE2_KEY1
}};
And here is where the problem arises: This compiles in both gcc 4.8.5 (CentOS 7.5 vanilla) and AppleClang 9.1.0.9020039 (MacOSX 10.3), but in MacOSX, the resulting set only contains a ".". It's like the static strings defined in the modules headers (MODULE1_NAME, MODULE1_KEY1, etc) are empty!!! In gcc/CentOS, it works fine. Unfortunately, I couldn't compile on gcc for MacOSX (It complains about unresolved dependencies with Boost), while CLang on CentOS (Apple LLVM version 9.1.0 (clang-902.0.39.2)) works fine too.
Do you have any clues what am I doing wrong? Is it something about using strings as static variables? I also tried declaring the set using the new initializer list syntax, const std::set<std::string> ParametersParser<T>::ALLOWED_PARAMS{{...}}, but didn't work either.
Thanks a lot for your help.

Related

code within statically linked library src files returning null

This is for windows using MSVC compiler version 14.28.29910:
Libraries built using colcon. This was meant for a ROS2 application but I dont believe ROS has anything to do with it.
I have been stuck on this issue for two days now and I still am at a loss as to what is going on. Any help will be greatly appreciated.
I have a library that I am statically linking against. Library A. it is built with colcon. Could this be a linking issue or an issue with the fact that I build library A with a certain set of preprocessors and I build library B with a different set of preprocessors that change the Gameobject class to a different version shown below but same function implementations.
ament_auto_add_library(A STATIC
${SOURCES}
${HEADERS}
)
ament_target_dependencies(A ${ALL_DEPENDS})
install(TARGETS
A
EXPORT A_export
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
ament_export_libraries(A_export HAS_LIBRARY_TARGET)
I then link to it from a ros2 package using the standard
target_link_libraries(B A)
I have a object called Gameobject that is defined in library A.
#file --------------------------Gameobject.h
#ifdef InLibraryA
class Gameobject
{
int GetPosx(){return data.x;}
}
#else
class Gameobject
{
int GetPosx(){return data.x;}
}
#endif
#file --------------------------MoveGameObject.h
class MoveGameObject
{
int MoveGObj_inline(Gameobject* g)
{
return g->getPosx();
}
int MoveGObj(Gameobject* g);
}
#file --------------------------MoveGameObject.cpp
int MoveGameObject::MoveGObj(Gameobject* g)
{
return g->getPosx();
}
Now in library B , I do this within a subscription callback
SomeCallback()
{
Gameobject* g = GetGObjectFromPool();
MoveGameObject* m= new MoveGameObject();
//this will return NULL value
int posx = m->MoveGObj(g);
//this will be fine because it was inlined?
int possx = m->MoveGObj_inline(g);
}
You will see that I get null when calling the function that was NOT inlined for calling the getter function from Gameobject. I dont get null for the inlined function even though they run the exact same code. Note that this only happens to non-inline functions that call Gameobject functions. Does not happen to functions that do not read memory from Gameobject. addingTwoInts() for example works fine Non-inline. There are no errors. It is undefined behavior. Any ideas on what I could be doing wrong for this to happen? The simplified code above is the same as to what is happening in my code, just removed unnecessary details.

Compile error when try to create singleton using shared_ptr

I try to create a singleton object using shared_ptrs. However the code does not compile when the constructors/destructor are private for the specific object
The code is below.h
//ThreadPool.h
class ThreadPool
{
public:
static std::shared_ptr<ThreadPool> & getInstance();
inline static std::shared_ptr<ThreadPool> m_threadPoolInstance;
private:
ThreadPool() =default;
~ ThreadPool() = default;
ThreadPool(ThreadPool const &) = default;
};
//ThreadPool.cpp
#include "pch.h"
#include <ThreadPool.h>
std::shared_ptr<ThreadPool> & ThreadPool::getInstance()
{
if (! m_threadPoolInstance)
{
ThreadPool * p_ThreadPool = new ThreadPool();
m_threadPoolInstance.reset(p_ThreadPool);
}
return m_threadPoolInstance;
}
I am using VS17 compiler
The error that is created is the following
error C2440: '': cannot convert from '_Ux *' to
'std::shared_ptr'
with
[
_Ux=ThreadPool
] include\memory(1462): note: No constructor could take the source type, or constructor overload resolution was ambiguous
threadpool.cpp(9): note: see reference to function template
instantiation 'void std::shared_ptr::reset(_Ux
*)' being compiled
with
[
_Ux=ThreadPool
] threadpool.cpp(9): note: see reference to function template instantiation 'void std::shared_ptr::reset(_Ux
*)' being compiled
with
[
_Ux=ThreadPool
]
When I set the constructors/destructor in public section, the compilation is succesfull.
However running the same code using gcc compiler , compiles succesfully
The conversion fails because your ThreadPool class has a private destructor.
Calling .reset(ptr) will use the delete expression (delete ptr;) as the deleter, which requires that the destructor be public.
Refer to overload (2) here: https://en.cppreference.com/w/cpp/memory/shared_ptr/reset
2-4) Replaces the managed object with an object pointed to by ptr. Y must be a complete type and implicitly convertible to T. Additionally:
2) Uses the delete expression as the deleter. A valid delete expression must be available, i.e. delete ptr must be well formed, have well-defined behavior and not throw any exceptions. Equivalent to shared_ptr(ptr).swap(*this);.
You either need to make the destructor public or provide a custom deleter.

Argument of function is being corrupted when function is exported in C++ when using extern "C" __declspec(dllexport)

I came across a code as follows:
#define FUNCTION_EXPORT extern "C" __declspec(dllexport)
typedef int32 (*U_Function)(HINSTANCE, const std::string &);
const char *P_FUNCTION = "processFunc";
Now this is being used in another part of same file:
int32 run(HINSTANCE instance, const std::string &cmdLine) {
try {
LibraryManager lib(PROC_LIBRARY);
FARPROC ptr = GetProcAddress(lib.getLibHandle(), U_FUNCTION);
if (ptr) {
result = reinterpret_cast<U_Function>(ptr)(instance, cmdLine);
}
.....
.....
so on
}
Now this is being used in another file .cpp of same project as following the reinterpret_cast step during debugging in visual studio in release mode:
FUNCTION_EXPORT int processFunc(HINSTANCE instance, const string &cmdLine) {
......
......
}
Here the cmdLine argument is being corrupted when function export is being used.
The cmdLine is given by user. For example if the user gives cmdLine as "None Stay" (the cmdLine was watched during debugging; as the first four bytes being randomly filled like 'su\0 and then the given argument "None Stay" follows in cmdLine) thus having only value 'su during runtime causing the application to fail.(it is being randomly generated, each time a different value following by None Stay like "¨‘\x16\x1NONE STAY etc.)
Note: The ptr and ProcessFunc have same value in run time watch.
How do we make that the user entered cmdLine argument is being successfully used during runtime and prevent the application from failing?

using a globals struct in several .cpp files, and initializing it in the constructor

I know similar questions have been asked, but none address this issue. I want to create a globals struct and initialize it with default values. I implemented it as below, but the project won't build.
I've tried everything I can think of, most notably moving the "extern" declaration of *gxg in and out of the header guard and changing the struct to a class, but get the same results: the project won't build because of duplicate symbols for the globals constructor. It builds if I don't use it in more than one .cpp file, or if I don't include a constructor or destructor in the struct's implementation file.
// globals.hpp
#ifndef globals_hpp
#define globals_hpp
struct gxGlobals{
double radius;
bool easement;
gxGlobals(); // constructor
} ;
extern "C" gxGlobals *gxg;
#endif /* globals_hpp */
—————————————
// globals.cpp
#include "globals.hpp"
gxGlobals::gxGlobals():
radius(24),
easement(false)
{};
———————————
// main_file.cpp
#include "globals.hpp"
gxGlobals *gxg = new gxGlobals();
———————————
// other_file.cpp
#include "globals.hpp"
// ERROR: Duplicate symbol gxGlobals::gxGlobals()
I can include globals.h in one file, but not in two or more. It also works if I remove the self-initialization in the .cpp file.
There are too many members in the actual struct to make an initializer list practical, so my last option is a function that runs on startup that plugs all of the default values in. Am I mistaken that this should work?

extern template DLLs and programs (crypto++)

I've been using Crypto++ with VS2005 and VS2010 for a while now. But recently I needed to use it with and application directly. The same code compiles fine when I'm compiling as a DLL and does not compile when compiling as an application.
This is the smallest sample that reproduces the error is this (based on cryptopp561\algparam.h:301 CryptoPP::AlgorithmParametersTemplate
class Base
{
protected:
virtual void MoveInto(void *p) const = 0;
};
template<class T>
class Test: public Base
{
public:
void MoveInto(void * buffer) const
{
Test<T> *x = new(buffer) Test<T>(*this);
}
};
extern template class Test<bool>;
The compilation parameters are the same, only difference that I saw was the configuration type in the project ("Application (.exe)" generates the error and "Dynamic Library (.dll)" does not).
This is the compiler error:
main.h(15): error C2061: syntax error : identifier 'buffer'
main.h(14) : while compiling class template member function 'void Test<T>::MoveInto(void *) const'
with
[
T=bool
]
main.h(20) : see reference to class template instantiation 'Test<T>' being compiled
with
[
T=bool
]
It seems to occur only when theres inheritance. Ommiting : public Base in the class Test declaration makes the error go away.
EDIT:
The problem was in a header included somewhere that defined a a debug version for operator new but didn't declared the placement new version.
Did you #include <new>, the header file that declares placement-new?
Funnily, extern templates are to tell the compiler to not instantiante at some point, so the second error does not make sense to me. Are you certain your compiler has support for extern templates? What if you do the opposite, explicit instantiation:
template class Test<bool>;

Resources