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 7911175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:09:05+00:00 2026-06-03T13:09:05+00:00

I’m converting a C++ program over from Windows building with Visual C++ 2008 to

  • 0

I’m converting a C++ program over from Windows building with Visual C++ 2008 to build on Linux with gcc 4.6.1. There’s a module that uses <unordered_map>. In VC++, it seems to be perfectly ok to

#include <unordered_map>

...



std::tr1::unordered_map<mystruct, int> my_map;

We’re actually supporting more compilers than just gcc 4.6 and VC++ 2008, so using pure C++2011 code isn’t feasible. gcc gets upset with #include <unordered_map>, complaining that this is the true-blue c++2011 include file, so one of the things I had to do to make this work was change the include to

#include <tr1/unordered_map>

...


std::tr1::unordered_map<mystruct, int> my_map;

This works. Fair enough. Now I’ve got another problem, though. Here’s the definition for mystruct:

struct mystruct
{
#ifdef __cplusplus
    inline operator size_t() const
    {
        return m_val;
    }
#endif
    unsigned int m_val;
};

In VC++ 2008, this seems to be as much as std::hash needs to specialize on mystruct. std::tr1::hash, on the other hand, doesn’t like this, at least not on gcc 4.6.1. It refuses to link, complaining that std::tr1::hash<mystruct>::operator()( mystruct ) const is undefined. I’m not sure whether that happens with VC++ when I do the proper tr1 include – maybe it’ll complain about the same thing? I’ll try it tomorrow, but for now all I’ve got is a linux box with gcc on it. For now, I had to do this to get it working:

namespace std {
    namespace tr1 {
        std::size_t hash<mystruct>::operator()( mystruct & c ) const 
        { 
            return c.m_val; 
        }
    }
}

Can anyone enlighten me on how this is supposed to work? It seems so much more elegant to be able to define a size_t operator on a type that you want to be hashable, but I’m willing to live with defining the operator() on std::tr1::hash.

UPDATE:

I tried specializing the entire hash class, as suggested. Building with gcc, I get

myfile.cpp:41:12: error: specialization of 'std::tr1::hash<mystruct>' after instantiation
myfile.cpp:41:12: error: redefinition of 'struct std::tr1::hash<mystruct>'
/usr/include/c++/4.6/tr1/functional_hash.h:45:12: error: previous definition of 'struct std::tr1::hash<mystruct>'
  • 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-06-03T13:09:07+00:00Added an answer on June 3, 2026 at 1:09 pm

    Microsoft’s way, to accept an implicit conversion to std::size_t, is an extension.

    GCC’s way, to specialize std::tr1::hash, is the way actually defined by TR1 which is now standardized in C++11 (of course with the tr1:: part dropped).

    MSVC should still accept the hash specialization, and of course both should accept a completely new class passed as the Hasher template argument.

    It is better style to specialize the entire class, not just the operator() function:

    namespace std {
        namespace tr1 {
            template<>
            struct hash< mystruct > {
                std::size_t operator()( mystruct & c ) const 
                { 
                    return c.m_val; 
                }
            };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have a text area in my form which accepts all possible characters from

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.