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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:50:59+00:00 2026-06-03T04:50:59+00:00

I have a template class Baz which contains a nested class Sub . I’d

  • 0

I have a template class Baz which contains a nested class Sub. I’d like to define a hash function for this subclass by specializing std::hash. However, it doesn’t seem to work.

#include <functional>

struct Foo {
    struct Sub {
    };
};

template <class T>
struct Bar {
};

template <class T>
struct Baz {
    struct Sub {
        int x;
    };
};

// declare hash for Foo::Sub - all right
namespace std {
    template <>
    struct hash< Foo::Sub >;
}

// declare hash for Bar<T> - all right
namespace std {
    template <class T>
    struct hash< Bar<T> >;
}

// declare hash function for Baz<T>::Sub - doesn't work!
namespace std {
    template <class T>
    struct hash< Baz<T>::Sub >;
}

// Adding typename produces a different error.
namespace std {
    template <class T>
    struct hash< typename Baz<T>::Sub >;
}

Gcc 4.5.3 complains:

$ g++ -std=c++0x -c hash.cpp
hash.cpp:34:30: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp> struct std::hash’
hash.cpp:34:30: error:   expected a type, got ‘Baz<T>::Sub’
hash.cpp:40:12: error: template parameters not used in partial specialization:
hash.cpp:40:12: error:         ‘T’

UPDATE

What I’m really trying to do is implement a container which supports stable references (not in the C++ sense) to elements within it. I want to allow the user to insert these references into std::unordered_set and similar, and use them to access or modify existing elements efficiently. The following is just a mockup, not the exact container I’m implementing. The problem is in defining a hash function for the reference type.

template <class T>
class Container {
public:
    class Reference {
    public:
        // operator==, operator!=, operator< ...., isNull()
    private:
        size_t index; // index into m_entries (or could be anything else)
        // possibly more stuff
    };

    Reference insert (const T &value);
    Reference find (const T &value);
    void remove (Reference r);
    Reference first ();
    Reference next (Reference prev);

private:
    struct Entry { T value, ... };

    std::vector<Entry> m_entries;
};
  • 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-03T04:51:01+00:00Added an answer on June 3, 2026 at 4:51 am

    The answer to this question is that what you’re trying to do is simply impossible to do. The compiler cannot figure out what outer class contains a subtype. Consider why:

    struct outer1 { typedef int Sub; };
    struct outer2 { typedef int Sub; };
    

    How is the compiler supposed to figure out which outer you want when it gets a Sub? It cannot. There’s no possible way for this to work.

    In your case it could be remotely possible, though extremely difficult, to derive IFF Sub dependend upon T. But this would require the compiler to know where Sub comes from and it just doesn’t.

    So you simply cannot do this. No way, no how.

    If you need some generic approach to finding a hash function for your types then you’ll need to make a metafunction get_hash. It could look for a “hash_type” internal typedef by default and be overridden for the standard hashes. Lot of typing…

    Alternatively, put Sub out of the containing class as its own template and have a typedef there instead of inner class. Then you can specialize hash on your template.

    Otherwise, just supply the hash you know you need to the hash function parameter to the template you’re using.

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

Sidebar

Related Questions

I have a template class which goes like this: class Template{ public $pageTitle =
I have a template class which has a static pointer-to-member, like this: template<class T,
I have a template class in C++ that looks like this: template <typename T,
I have a template class that looks something like this: template<class T> class C
I have a template class, inside which i have a normal function. But i
I have a template class defined in a header file like this. Here I
I have a template class like this: template<T> class MyClass { T* data; }
I have a template class which implements function: template<typename T> class Matrix { ...
I have a template class which looks like the following: template <template <class TypeT>
I have a template class that I serialize (call it C), for which 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.