Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:29:55+00:00 2026-05-27T03:29:55+00:00

I have a shared library with class: // HPP: class WorldSettings { private: static

  • 0

I have a shared library with class:

// HPP:
class WorldSettings
{
    private:
        static std::unordered_map<std::string, int> mIntegerStorage;
        static std::unordered_map<std::string, float> mFloatStorage;
        static std::unordered_map<std::string, std::string> mStringStorage;

    public:
        template <typename T>
        static T &Get(const std::string &key);

        template <typename T>
        static T &Set(const std::string &key, T value);
};

// CPP:

#define DoReturn(MapName, Key, Type) {                                                \
    assert( MapName.find(Key) != MapName.end() && "No such key in world settings!" ); \
    return MapName[Key];                                                              \
}                                                                                     \

#define DoSetup(MapName, Key, Value) { \
    MapName[key] = Value;              \
    return MapName[Key];               \
}                                      \

std::unordered_map<std::string, int> WorldSettings::mIntegerStorage;
std::unordered_map<std::string, float> WorldSettings::mFloatStorage;
std::unordered_map<std::string, std::string> WorldSettings::mStringStorage;

// Getters

template <>
int &WorldSettings::Get<int>(const std::string &key)
{
    DoReturn(mIntegerStorage, key, int);
}

template <>
float &WorldSettings::Get<float>(const std::string &key)
{
    DoReturn(mFloatStorage, key, float);
}

template <>
std::string &WorldSettings::Get<std::string>(const std::string &key)
{
    DoReturn(mStringStorage, key, std::string);
}

// Setters

template <>
int &WorldSettings::Set<int>(const std::string &key, int value)
{
    DoSetup(mIntegerStorage, key, value);
}

template <>
float &WorldSettings::Set<float>(const std::string &key, float value)
{
    DoSetup(mFloatStorage, key, value);
}

template <>
std::string &WorldSettings::Set<std::string>(const std::string &key, std::string value)
{
    DoSetup(mStringStorage, key, value);
}

Now I want to use this class in non shared library (simple console application):

WorldSettings::Get<int>("WorldMinutes");

The ‘WorldMinutes’ is set in shared library code:

WorldSettings::Set<int>("WorldMinutes", 0);

The problem is:

Floating point exception

Program received signal SIGFPE, Arithmetic exception.
0x00000000004d1f61 in std::__detail::_Mod_range_hashing::operator() (this=0x747863, __num=732984944481197501, 
    __den=0) at /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/hashtable_policy.h:376
376     { return __num % __den; }

Backtrace:

#0  0x00000000004d1f61 in std::__detail::_Mod_range_hashing::operator() (this=0x747863, __num=732984944481197501, 
    __den=0) at /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/hashtable_policy.h:376
#1  0x00000000004d3503 in std::__detail::_Hash_code_base<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_bucket_index (this=0x747860, __c=732984944481197501, __n=0)
    at /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/hashtable_policy.h:758
#2  0x00000000004d2a9a in std::__detail::_Map_base<std::string, std::pair<std::string const, int>, std::_Select1st<std::pair<std::string const, int> >, true, std::_Hashtable<std::string, std::pair<std::string const, int>, std::allocator<std::pair<std::string const, int> >, std::_Select1st<std::pair<std::string const, int> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true> >::operator[] (this=0x747860, __k=...)
    at /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/hashtable_policy.h:543
#3  0x00000000004d1cfa in WorldSettings::Set<int> (key=..., value=0)

The error is called when Set<int>(...) in shared library code. The interesting thing is that I don’t have an error when call Get from library code. What could be wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:29:56+00:00Added an answer on May 27, 2026 at 3:29 am

    The code is incorrect as it is.

    The specializations you define in the .cpp file should have been declared (out of the class) in the .hpp file, otherwise any time you try to use the defined function you have undefined behavior.

    Add:

    template <>
    int &WorldSettings::Get<int>(const std::string &key);
    
    template <>
    float &WorldSettings::Get<float>(const std::string &key);
    
    template <>
    std::string &WorldSettings::Get<std::string>(const std::string &key);
    
    // Setters
    
    template <>
    int &WorldSettings::Set<int>(const std::string &key, int value);
    
    template <>
    float &WorldSettings::Set<float>(const std::string &key, float value);
    
    template <>
    std::string &WorldSettings::Set<std::string>(const std::string &key, std::string value);
    

    right after the class definition.

    This tells the compiler that the definitions are generated elsewhere.

    As it is, it is likely that the shared library will generate its own copy of the functions (did you provided some default template implementation ?) and that may cause havoc.

    It may not be the cause of the bug, but it does not make sense to investigate further.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code shared among multiple projects in a static library. Even with
Situation is following. I have shared library, which contains class definition - QueueClass :
I have a class with number of private data members (some of them static),
I have a class like class K { static int a; static int b;
Assuming I have a shared activity class defined in a Library project, which does
I have a class library shared between an Azure Worker Role and a ASP.NET
I have changed the package of a class in a shared library. I have
I'm developing a shared library. Let's say I have the following class definition: class
I have a shared library that I wish to link an executable against using
I have existing Linux shared object file (shared library) which has been stripped. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.