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

  • Home
  • SEARCH
  • 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 6125673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:16:30+00:00 2026-05-23T16:16:30+00:00

What is operator size_t () const Environment: Visual Studio 2010 Professional TL; DR Today

  • 0

What is

operator size_t () const

Environment: Visual Studio 2010 Professional


TL; DR

Today I was searching for a way to use std::tr1::unordered_set. Because I asked for how to use std::map last time, I decided to find it out myself.

I googled and most of the results told me to have a struct to do hashing. The way looked a little bit complicated for me, and I kept searching and finally came across another approach.

I need to implement

bool operator == (const edge & another) const

and

operator size_t () const

The resulting code is near the end of the question.

== is familiar without any problem. size_t is familiar, too. But what is operator size_t?

It seems like equals and hashCode for Java, which need to be overridden together according to Effective Java. But I am not sure, especially when the name is size_t.


The resulting code is as follows. The complete program works fine, and produces correct outputs.

class edge {
public:
    int x;
    int y;
    edge(int _x, int _y) : x(_x), y(_y) {
    }
    bool operator == (const edge & another) const {
        return (x == another.x && y == another.y);
    }
    operator size_t () const {
        return x * 31 + y;
    }
};

A little bit more:

Not

size_t operator () const

which cannot be compiled:

error C2143: syntax error : missing ';' before 'const'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2059: syntax error : '{'
error C2334: unexpected token(s) preceding '{'; skipping apparent function body

Even not

int operator size_t () const

but as I see it, the function returns int. The error code is as follows:

error C2549: user-defined conversion cannot specify a return type
  • 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-23T16:16:30+00:00Added an answer on May 23, 2026 at 4:16 pm

    It’s the type cast operator. Basically provides for implicitly converting the object to the specified type, in this case size_t.

    EDIT:

    Say you have a function defined as follows:

    void Foo( size_t x )
    {
      // do something with x
    }
    

    If your class edge defines the type-cast operator for conversion to size_t you can do the following:

    edge e;
    Foo( e );
    

    The compiler will automatically convert the edge object to size_t. As @litb says in the comments section, do not do this. Implicit conversions can cause trouble by allowing the compiler to perform conversions when you may not have intended for that to happen.

    You should instead define a member function like edge::to_size_t() (I know that’s a terrible name) to perform the conversion.

    As an example, std::string defines the std::string::c_str() member function instead of defining the type-cast operator for conversion to const char *.

    EDIT 2:
    Sorry, I didn’t read your question carefully enough. Now I see you’re trying to use your class in an std::unordered_set. In that case you should define functors that do the hash and compare operations for your class. Alternatively, you can provide template specializations of std::hash and std::equal_to for your class and not have to specify the optional template parameters when creating the unordered_set object.

    As you’ve asked in the question, this is very much like Java’s hashCode() member function, but since C++ classes do not all derive from a common base class like Java classes, it is not implemented as an override-able base class function.

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

Sidebar

Related Questions

if I'm using the sizeof operator and making use of size_t in my code,
Is this valid/portable/legal code: class Vector3 { public: float& operator [] ( const size_t
suppose I have an object like this: class Spline { public: Spline(std::size_t const dim);
bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
What is the operator precedence order in Visual Basic 6.0 (VB6)? In particular, for
I want to define std::tr1::hash<boost::tuple<A,B,C> > . But I get an error that doesn't
To support user-defined key types in std::unordered_set<Key> and std::unordered_map<Key, Value> one has to provide
I want to partially specialize an existing template that I cannot change ( std::tr1::hash
I'm trying to use an unordered_set to maintain an unique list of structs. I've
I'm loading a very long list from disk into an unordered_set. If I use

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.